在这篇文章中,我们将把剩下的功能完成,首先,我们加入换子弹或是炸弹的功能,它的原理大体是这样的,设置物品出现的时间间隔,会随机出现物品的种类,然后判断物品跟飞机所在区域,碰撞检测,在一起就算是吃到这个物品了,。。OK,下面上代码
首页新建一个类继承CCNode,
ChangeBullet.h
typedef enum{ propsTypeBomb = 4, propsTypeBullet = 5 }prosType; class ChangeBullet:public cocos2d::CCNode { public: cocos2d::CCSprite *prop; prosType bulletType; void initWithType(prosType type); void propAnimation(); static ChangeBullet* create(void); CC_SYNTHESIZE_RETAIN(cocos2d::CCSprite*, _prop, Prop); ChangeBullet(); ~ChangeBullet(); };ChangeBullet.cpp
ChangeBullet::~ChangeBullet() { } ChangeBullet::ChangeBullet() { } ChangeBullet* ChangeBullet::create() { ChangeBullet * pRet = new ChangeBullet(); if (pRet && pRet->init()) { pRet->autorelease(); } else { CC_SAFE_DELETE(pRet); } return pRet; } void ChangeBullet::initWithType(prosType type) { bulletType=type; CCString *string=CCString::createWithFormat("enemy%i_fly_1.png",type); prop=CCSprite::createWithSpriteFrameName(string->getCString()); prop->setPosition(ccp((arc4random()%268 + 23)*2,632*2)); } //物品出现动画 void ChangeBullet::propAnimation() { CCMoveTo *ac1=CCMoveTo::create(1, ccp(prop->getPosition().x, 250*2)); CCMoveTo *ac2=CCMoveTo::create(0.4, ccp(prop->getPosition().x, 252*2)); CCMoveTo *ac3=CCMoveTo::create(1, ccp(prop->getPosition().x, 632*2)); CCMoveTo *ac4=CCMoveTo::create(2, ccp(prop->getPosition().x, -55*2)); prop->runAction(CCSequence::create(ac1,ac2,ac3,ac4,NULL)); }
GameScene.h
void addBulletTypeTip(); //空降物品时间计数 int propTime; ChangeBullet *prop; bool isVisible; bool isBigBullet; bool isChangeBullet; //子弹持续时间 int bulletlastTime; void bulletLastTime();
//空降物 void GameLayer::addBulletTypeTip() { propTime++; if (propTime>500) { prop=ChangeBullet::create(); prop->initWithType((prosType)(arc4random()%2 + 4)); this->addChild(prop->prop); prop->propAnimation(); prop->retain(); propTime=0; isVisible=true; } } void GameLayer::bulletLastTime() { if (isBigBullet) { if (bulletlastTime > 0) { bulletlastTime --; } else { bulletlastTime = 1200; isBigBullet = false; isChangeBullet = true; } } }
this->addBulletTypeTip(); this->bulletLastTime();
在collisionDetection函数中
//飞机跟空降物 if (isVisible==true) { CCRect playRect=playSprite->boundingBox(); CCRect propRect=prop->prop->boundingBox(); if (playRect.intersectsRect(propRect)) { prop->prop->stopAllActions(); prop->prop->removeFromParentAndCleanup(true); isVisible=false; //换子弹 if (prop->bulletType==propsTypeBullet) { // CCLOG("--------22222"); isBigBullet = true; isChangeBullet = true; } //炸弹 else { for (int i=0; i<planeArray->count(); i++) { EnemyPlane *enemyPlane=(EnemyPlane *)planeArray->objectAtIndex(i); //爆炸动画 this->enemyPlaneBlowupAnimation(enemyPlane); } planeArray->removeAllObjects(); } } }
OK,到现在我们还没做自己飞机的碰撞,那么接下来我们在collisionDetection函数中,再加入一个判断,判断敌人的飞机跟我们自己的飞机是否碰撞了,上代码
CCRect playRec=playSprite->boundingBox(); playRec.origin.x += 25*2; playRec.size.width -= 50*2; playRec.origin.y -= 10*2; playRec.size.height -= 10*2; for (int i=0; i<planeArray->count(); i++) { EnemyPlane *enemyPlane=(EnemyPlane *)planeArray->objectAtIndex(i); if (playRec.intersectsRect(enemyPlane->boundingBox())) { this->playerBlowupAnimation(); this->enemyPlaneBlowupAnimation(enemyPlane); this->gameOver(); planeArray->removeObject(enemyPlane); } }
//自己的飞机爆炸动画 void GameLayer::playerBlowupAnimation() { playSprite->stopAllActions(); CCArray *array=CCArray::create(); for (int i=1; i<=4; i++) { CCString *string=CCString::createWithFormat("hero_blowup_%i.png",i); CCSpriteFrame *frame=CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(string->getCString()); array->addObject(frame); } CCAnimation *animation=CCAnimation::createWithSpriteFrames(array,0.1); CCAnimate *animate=CCAnimate::create(animation); CCRepeatForever *ac1=CCRepeatForever::create(animate); playSprite->runAction(ac1); array->removeAllObjects(); }
//游戏结束 void GameLayer::gameOver() { isGameOver=true; this->gamePause(); CCLayerColor *gameoverLayer=CCLayerColor::create(ccc4(215, 221, 222, 255), 582, 544); gameoverLayer->setPosition(ccp(wSize.width/2-582/2, 220)); this->addChild(gameoverLayer, 3); CCLabelTTF *ttfLabel=CCLabelTTF::create("飞机大战分数", "MarkerFelt-Thin", 50); ttfLabel->setPosition(ccp(gameoverLayer->getContentSize().width/2-ttfLabel->getContentSize().width/2, gameoverLayer->getContentSize().height-70)); ttfLabel->setColor(ccc3(0, 0, 0)); ttfLabel->setAnchorPoint(ccp(0, 0)); gameoverLayer->addChild(ttfLabel, 1); CCLabelTTF *scLabel=CCLabelTTF::create(scoreLabel->getString(), "MarkerFelt-Thin", 44); scLabel->setPosition(ccp(gameoverLayer->getContentSize().width/2-scLabel->getContentSize().width/2, gameoverLayer->getContentSize().height-250)); scLabel->setColor(ccc3(0, 0, 0)); scLabel->setAnchorPoint(ccp(0, 0)); gameoverLayer->addChild(scLabel, 1); CCMenuItemFont *startItem=CCMenuItemFont::create("继续", this,menu_selector(GameLayer::restart)); startItem->setPosition(ccp(gameoverLayer->getContentSize().width/2, 60)); startItem->setFontSizeObj(50); startItem->setFontNameObj("Georgia-Bold"); startItem->setColor(ccc3(0, 0, 0)); CCMenu *pMenu = CCMenu::create(startItem, NULL); pMenu->setPosition(CCPointZero); gameoverLayer->addChild(pMenu, 1); } //游戏暂停 void GameLayer::gamePause() { if (isGameOver==false) { } else { CCObject *object; CCARRAY_FOREACH(this->getChildren(), object) { CCNode *node=(CCNode *)object; node->stopAllActions(); } } }
OK.。。。到这里,基本上大体的功能有了,呵呵,貌似暂停还木有。。。。
下篇文章中,我们将来探究一下在ios平台上适配的问题,当然还有android等其他平台的适配,。。。。。。。。。