显示出来,显示完毕后删除图片,并且删除敌机以及我机子弹。今天做了飞机的生命值,在敌机子弹和我机进行碰撞时,进行一个飞机生命值变量的递减效果,当变量==0的时候,即飞机死亡,跳转第三个场景,
显示gameover。分数同样原理可以实现,在我机子弹和敌机发生碰撞以后,进行分数变量的递增效果。
void GameScene::boom(Sprite *p){
Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
SpriteFrameCache *frameCache = SpriteFrameCache::getInstance();
frameCache->addSpriteFramesWithFile("OnceEffect.plist", "OnceEffect.png");
this->Boom = p;
Boom = Sprite::createWithSpriteFrameName("OnceEffect_10_1.png");
this->addChild(Boom, 3, 1);
Boom->setPosition(Vec2(p->getPositionX(), p->getPositionY()));
Animation* animation = Animation::create();
animation->setDelayPerUnit(0.02f);
animation->addSpriteFrame(frameCache->getSpriteFrameByName("OnceEffect_13_1.png"));
animation->addSpriteFrame(frameCache->getSpriteFrameByName("OnceEffect_13_2.png"));
animation->addSpriteFrame(frameCache->getSpriteFrameByName("OnceEffect_13_3.png"));
animation->addSpriteFrame(frameCache->getSpriteFrameByName("OnceEffect_13_4.png"));
animation->addSpriteFrame(frameCache->getSpriteFrameByName("OnceEffect_13_5.png"));
animation->addSpriteFrame(frameCache->getSpriteFrameByName("OnceEffect_13_6.png"));
Animate* animate = Animate::create(animation);
EnemyBoom.pushBack(Boom);
for (int i = 0; i < EnemyBoom.size(); i++){
EnemyBoom.at(i)->runAction(Sequence::create(animate, CallFunc::create(CC_CALLBACK_0(GameScene::remove, this)), nullptr));
}
}
清除爆炸动画
void GameScene::remove(){
if (Boom != NULL){
EnemyBoom.at(0)->removeFromParent();
EnemyBoom.eraseObject(EnemyBoom.at(0));
}
}
防止游戏变卡,在我机子弹,敌机子弹,敌机超出屏幕时清除缓存
for (int i = 0; i < aaa.size(); i++){
if (aaa.at(i)->getPositionY() >= visibleSize.height + aaa.at(i)->getContentSize().height / 2){
a1->remove(aaa.at(i));
}
}
for (int i = 0; i < bbb.size(); i++){
if (bbb.at(i)->getPositionY() <= - bbb.at(i)->getContentSize().height / 2){
b1->remove(bbb.at(i));
}
}
for (int i = 0; i < ccc.size(); i++){
if (ccc.at(i)->getPositionY() <= -ccc.at(i)->getContentSize().height / 2){
b1->remove1(ccc.at(i));
}
}
for (int i = 0; i < eee.size(); i++){
if (eee.at(i)->getPositionY() <= -eee.at(i)->getContentSize().height / 2){
d1->remove5(eee.at(i));
}
}
//我方子弹和敌机碰撞
for (int i = 0; i < aaa.size(); i++)
{
for ( j = 0; j < bbb.size(); j++)
{
if (aaa.at(i)->getBoundingBox().intersectsRect(bbb.at(j)->getBoundingBox()))
{
fenshu+=10;
boom(bbb.at(j));//爆炸效果
a1->remove(aaa.at(i));
b1->remove(bbb.at(j));
char string[20] = { 0 };
sprintf(string, "fenshu:%d", fenshu);
auto label2 = (Label*)getChildByTag(5);
label2->setString(string);
添加文本,显示 分数
}
}
}
//我方子弹和敌机碰撞
添加文本,显示我机的PH
for (int i = 0; i < aaa.size(); i++)
{
for ( j = 0; j < bbb.size(); j++)
{
if (aaa.at(i)->getBoundingBox().intersectsRect(bbb.at(j)->getBoundingBox()))
{
fenshu+=10;
boom(bbb.at(j));//爆炸效果
a1->remove(aaa.at(i));
b1->remove(bbb.at(j));
char string[20] = { 0 };
sprintf(string, "fenshu:%d", fenshu);
auto label2 = (Label*)getChildByTag(5);
label2->setString(string);
}
}
}