做这个飞机游戏,花了将近一周的时间,今天看起来才有游戏的样子,做游戏过程是漫长的,结局让人兴奋,想想自己可以让毫无生机的图片动起来,想想就激动。
今天实现飞机大战的积分的系统,英雄的生命值,还有就是添加 了一个道具。
积分系统:
//添加一个积分的lable
auto score2 = Label::create();
score2 = Label::createWithBMFont("boundsTestFont.fnt", "red");
addChild(score2, 6, 66);
score2->setAnchorPoint(Vec2(0,0));
score2->setPosition(Vec2(25, 420));
void GameScene::getScore(){
char string[20] = {0};
sprintf(string, "Score:%-1d", score);
//sprintf(string, "heroHP:%d", heroHP);
auto lable = (Label*)getChildByTag(66);
lable->setString(string);
}
积分就是设置一个文本匡,在子弹碰到敌人飞机的时候分数就会现实++,值得注意是文本匡在哪用就在哪调用,不然会出现重复打印的现象。
英雄的生命值就是,定义一个全局的变量,在英雄遇到攻击的时候,就会出现生命值的的减少,这样就可以在文本匡中实现看分数了。
if (enem_2Vec.at(i)->getBoundingBox().intersectsRect(hero->getBoundingBox())){
heroHP-=1;
//当英雄飞机遇到攻击时添加飞机的闪烁效果
auto action = Blink::create(2, 5);
hero->runAction(action);
if (heroHP<=0)
{
//在英雄遇到碰撞时
hero->removeFromParent();
hero = NULL;
enem_2Vec.at(i)->removeFromParent();
enem_2Vec.eraseObject(enem_2Vec.at(i));
//在游戏结束的时候关闭所有计时器
this->unscheduleAllSelectors();
//跳转到开始界面
auto scene = HelloWorld::createScene();
Director::getInstance()->replaceScene(TransitionFadeDown::create(1, scene));
//enem_2Vec.eraseObject(enem_2Vec.at(i));
break;
下午的时间还做了一个简单 的道具,这个道具是给英雄添加生命值的。道具对的应用是在碰撞的时候,使生命值做加加的操作,这样就可以,添加道具。
void GameScene::heroDo(float dt3){
//添加道具
Size visibleSize = Director::getInstance()->getVisibleSize();
herodo = Sprite::create("daoju.png");
visibleSize.width = rand() % 600;
herodo->setPosition(Vec2(visibleSize.width / 2, visibleSize.height));
this->addChild(herodo, 5);
herodoVec.pushBack(herodo);
//enem_2Vec.pushBack(enem_2);
//设置道具的终点位置
auto moveenem_3 = MoveTo::create(3.0f, Vec2(rand() % 600, -herodo->getContentSize().height / 2));
herodo->runAction(moveenem_3);
}
//道具和我方英雄的碰撞
for (int i = 0; i < herodoVec.size(); i++)
{
if (herodoVec.at(i)->getBoundingBox().intersectsRect(hero->getBoundingBox()))
{
herodoVec.at(i)->removeFromParent();
herodoVec.eraseObject(herodoVec.at(i));
heroHP += 2;
return;
}
}
下一步想做的就是控制BOSS的出现的时间,对于这个还是没有实现。