转载请注明出处:http://blog.csdn.net/oyangyufu/article/details/26942311
源代码地址:http://download.csdn.net/detail/oyangyufu/7409593
Ccp文件介绍:
GameMenuScene.cpp游戏主菜单
GameMainScene.cpp游戏主页面
GameObjHero.cpp主角
GameHeroBullet.cpp主角的子弹
GameObjEnemy.cpp敌人
GameEnemyBullet.cpp敌人的子弹
GameAboutScene.cpp关于页面
GameMark.cpp游戏分数
执行游戏进入游戏主菜单页面
代码:
GameMenuScene类init()函数,初始化主菜单页面背景图
CCSprite* back = CCSprite::create("bg.png"); back->setScale(0.5); back->setPosition(ccp(size.width/2,size.height/2)); this->addChild(back,0,0);
初始化主菜单页面背景星球图片
CCSprite* bgstar = CCSprite::create("moon.png"); bgstar->setAnchorPoint(ccp(0.5,0)); bgstar->setScale(0.5); bgstar->setPosition(ccp(size.width/3 * 2, 0)); this->addChild(bgstar,1,1);
初始化主菜单页面标题名称meowstar、battle
CCNode* title = CCNode::create(); title->setContentSize(CCSizeMake(0,0)); CCSprite* ptmLabel = CCSprite::create("meowstar.png"); ptmLabel->setScale(0.5); ptmLabel->setPosition(ccp(0,30)); title->addChild(ptmLabel); CCSprite *ptbLabel = CCSprite::create("battle.png"); ptbLabel->setScale(0.5); ptbLabel->setPosition( ccp(0,-30) ); title->addChild(ptbLabel); title->setPosition(ccp(size.width / 2, size.height - 80)); this->addChild(title,2,2);
初始化主菜单页面中的菜单项及单击事件,并设置为不可见
CCMenuItemImage *newGameItem = CCMenuItemImage::create("newgameA.png", "newgameB.png",this,menu_selector(GameMenuScene::menuNewGameCallback));//newgame菜单项 newGameItem->setScale(0.5); newGameItem->setPosition(ccp(size.width / 2,size.height / 2 - 20)); newGameItem->setEnabled(false); CCMenuItemImage *continueItem = CCMenuItemImage::create("continueA.png", "continueB.png",this,menu_selector(GameMenuScene::menuContinueCallback));//continue菜单项 continueItem->setScale(0.5); continueItem->setPosition(ccp(size.width / 2,size.height / 2 - 80)); continueItem->setEnabled(false); CCMenuItemImage *aboutItem = CCMenuItemImage::create("aboutA.png", "aboutB.png",this,menu_selector(GameMenuScene::menuAboutCallback));//aboutA菜单项 aboutItem->setScale(0.5); aboutItem->setPosition(ccp(size.width / 2,size.height / 2 - 140)); aboutItem->setEnabled(false); m_soundItem = CCMenuItemImage::create("sound-on-A.png", "sound-on-B.png",this,menu_selector(GameMenuScene::menuSoundCallback));//设置声音菜单项 m_soundItem->setScale(0.5); m_soundItem->setEnabled(false); m_soundItem->setPosition(ccp(40,40)); CCMenuItemImage *pCloseItem = CCMenuItemImage::create( "CloseNormal.png", "CloseSelected.png", this, menu_selector(GameMenuScene::menuCloseCallback)); pCloseItem->setPosition(ccp(size.width - 20, 20)); //使用button创建菜单 CCMenu* mainmenu = CCMenu::create(newGameItem,continueItem,aboutItem,m_soundItem,pCloseItem,NULL); mainmenu->setPosition(ccp(0,0)); this->addChild(mainmenu,1,3);
初始化背景音乐
m_isSound = true;//是否开启声音标识 CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic( CCFileUtils::sharedFileUtils()->fullPathForFilename("background.mp3").c_str() ); CocosDenshion::SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(0.5); CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic(CCFileUtils::sharedFileUtils()->fullPathForFilename("background.mp3").c_str(), true);
进入主菜单页面有动画效果
void GameMenuScene::onEnter() { CCLayer::onEnter(); CCSize size = CCDirector::sharedDirector()->getWinSize(); CCNode* mainmenu = this->getChildByTag(3);//主菜单 mainmenu->setPositionX(-200); mainmenu->runAction(CCSequence::create(CCEaseElasticOut::create(CCMoveTo::create(1,ccp(0,0))),CCCallFuncN::create(this, callfuncN_selector(GameMenuScene::menuEnter)),NULL));//终点有弹力 CCNode* title = this->getChildByTag(2);//标题 title->setPositionY(size.height + 20); title->runAction(CCEaseElasticOut::create(CCMoveBy::create(1,ccp(0,-100)))); CCNode* bgstar = this->getChildByTag(1);//星球 bgstar->setPositionX(size.width/3); bgstar->runAction(CCMoveBy::create(0.5,ccp(size.width/3,0))); } void GameMenuScene::menuEnter( CCNode* pSender ) { //菜单项可见 CCNode* mainmenu = this->getChildByTag(3); CCArray* temp = mainmenu->getChildren(); for(unsigned int i = 0;i < temp->count();i ++) { ((CCMenuItemImage *)temp->objectAtIndex(i))->setEnabled(true); } }
点击NEW GAME、Contunue菜单按键切换GameMainScene场景进入游戏主页面
void GameMenuScene::menuNewGameCallback( CCObject* pSender ) { CCDirector::sharedDirector()->replaceScene(GameMainScene::scene()); } void GameMenuScene::menuContinueCallback( CCObject* pSender ) { CCDirector::sharedDirector()->replaceScene(GameMainScene::scene()); }
点击About 菜单按键切换GameAboutScene场景进入关于页面
void GameMenuScene::menuAboutCallback( CCObject* pSender ) { CCDirector::sharedDirector()->replaceScene(GameAboutScene::scene()); }
点击声音菜单按键,变换按键图标,设置声音开关
void GameMenuScene::menuSoundCallback( CCObject* pSender ) { if(!m_isSound)//声音开 { m_soundItem->setNormalImage(CCSprite::create("sound-on-A.png")); m_soundItem->setDisabledImage(CCSprite::create("sound-on-B.png")); CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic(CCFileUtils::sharedFileUtils()->fullPathForFilename("background.mp3").c_str(), true); m_isSound = true; } else//声音关 { m_soundItem->setNormalImage(CCSprite::create("sound-off-A.png")); m_soundItem->setDisabledImage(CCSprite::create("sound-off-B.png")); CocosDenshion::SimpleAudioEngine::sharedEngine()->stopBackgroundMusic(); m_isSound = false; } }
关于页面
代码:
初始化关于页面
背景
CCSprite* bg = CCSprite::create("bg.png"); bg->setScale(0.5); bg->setPosition( ccp(size.width/2, size.height/2) ); this->addChild(bg, 0,0);
星球
CCSprite*bgstar = CCSprite::create("moon.png"); bgstar->setAnchorPoint(ccp(0.5,0)); bgstar->setScale(0.5); bgstar->setPosition(ccp(size.width/3 * 1, 0)); this->addChild(bgstar,1,1);
关于页面的信息
CCSprite*kuang = CCSprite::create("tb.png"); kuang->setScale(0.5); kuang->setPosition(ccp(size.width/2, size.height/2)); this->addChild(kuang,2,2); char inf[256]; sprintf(inf,"游戏名称:喵星战争\n\n作者:\n\n公司:\n\n使用工具:cocos2D-x"); CCLabelTTF * myjineng = CCLabelTTF::create(inf,"Marker Felt",40,CCSizeMake(400,400),kCCTextAlignmentLeft); myjineng->setAnchorPoint(ccp(0,1)); myjineng->setColor(ccc3(255,200,0)); myjineng->setPosition(ccp(50,600)); kuang->addChild(myjineng);//将标签增加关于框
返回菜单按键,并注冊点击事件
CCMenuItemImage *back = CCMenuItemImage::create("backA.png", "backB.png",this,menu_selector(GameAboutScene::menuBackCallback)); back->setScale(0.5); back->setPosition(ccp(size.width - 20,size.height - 20)); back->setEnabled(false); CCMenu* mainmenu = CCMenu::create(back,NULL); mainmenu->setPosition(ccp(0,0)); this->addChild(mainmenu,3,4);
返回键点击返回到主菜单页面
void GameAboutScene::menuBackCallback( CCObject* pSender ) { CCDirector::sharedDirector()->replaceScene(GameMenuScene::scene()); }
进入关于页面时的动画
void GameAboutScene::onEnter() { CCLayer::onEnter(); //菜单进入后,菜单项点击有效 CCSize size = CCDirector::sharedDirector()->getWinSize(); CCNode* mainmenu = this->getChildByTag(4);//返回键 mainmenu->setPositionX(-100); mainmenu->runAction(CCSequence::create(CCEaseElasticOut::create(CCMoveBy::create(0.5,ccp(100,0))),CCCallFuncN::create(this, callfuncN_selector(GameAboutScene::menuEnter)),NULL));//终点弹力效果 // CCNode* title = this->getChildByTag(3);//关于页面标题 title->setPositionY(size.height+20); title->runAction(CCEaseElasticOut::create(CCMoveBy::create(0.5,ccp(0,-40)))); CCNode*bgstar = this->getChildByTag(1); bgstar->setPositionX(size.width/3 * 2); bgstar->runAction(CCMoveBy::create(0.5,ccp(-size.width/3,0))); CCNode*kuang = this->getChildByTag(2);//关于框 kuang->setPositionX(-200); kuang->runAction(CCEaseElasticOut::create(CCMoveTo::create(0.5,ccp(size.width/2,size.height/2)))); }
进入主页面
代码:
初始化游戏主页面
背景,创建2个图拼接一起
m_bg1 = CCSprite::create("bg.png"); m_bg1->setScale(0.5); m_bg2 = CCSprite::create("bg.png"); m_bg2->setScale(0.5); m_bg1->setAnchorPoint(ccp(0,0));//设置锚点 m_bg2->setAnchorPoint(ccp(0,0)); m_bg1->setPosition( ccp(0,0) ); m_bg2->setPosition( ccp(0,size.height) ); this->addChild(m_bg1, 0); this->addChild(m_bg2, 0);
创建主角及出场动画
m_hero = new GameObjHero(); m_hero->setPosition(ccp(size.width/2,-50)); m_hero->setScale(0.5); this->addChild(m_hero,2,1); m_hero->runAction(CCMoveBy::create(0.5,ccp(0,150)));
创建3个敌人,每一个敌人有移动动作
m_enemys = CCArray::createWithCapacity(3); for(int i = 0;i < 3;i ++) { int type = i + 1; GameObjEnemy* enemy = new GameObjEnemy(); enemy->setPosition(ccp(size.width/4 * type,size.height + 50)); enemy->setScale(0.5); enemy->setType(type); m_enemys->addObject(enemy); this->addChild(enemy,1); enemy->moveStart(); }
初始主角生命数及生命图标
m_blood = 3; CCSpriteBatchNode* ui = CCSpriteBatchNode::create("cat.png"); //CCNode *ui = CCNode::create(); m_blood1 = CCSprite::createWithTexture(ui->getTexture()); m_blood1->setPosition(ccp(20,size.height - 20)); m_blood1->setScale(0.2f); ui->addChild(m_blood1); m_blood2 = CCSprite::createWithTexture(ui->getTexture()); m_blood2->setPosition(ccp(50,size.height - 20)); m_blood2->setScale(0.2f); ui->addChild(m_blood2); m_blood3 = CCSprite::createWithTexture(ui->getTexture()); m_blood3->setPosition(ccp(80,size.height - 20)); m_blood3->setScale(0.2f); ui->addChild(m_blood3); addChild(ui,4);
初始化主角子弹
m_heroBullets = CCArray::createWithCapacity(5); for(size_t i = 0;i < m_heroBullets->capacity();i ++) { GameHeroBullet * mybullet = new GameHeroBullet(); mybullet->setIsNotVisable(this); mybullet->setScale(0.5); m_heroBullets->addObject(mybullet); this->addChild(mybullet,3); } m_heroBullets->retain();//计数器+1
初始化敌人子弹
m_enemyBullets = CCArray::createWithCapacity(10); for(size_t i = 0;i < m_enemyBullets->capacity();i ++) { GameEnemyBullet * mybullet = new GameEnemyBullet(); mybullet->setIsNotVisable(this);//若是没加object參数,会发生执行时调用约定错误,原因未知 mybullet->setScale(0.5); m_enemyBullets->addObject(mybullet); this->addChild(mybullet,3); } m_enemyBullets->retain();
游戏分数
m_gameMark = new GameMark(); this->addChild(m_gameMark,4);
初始化游戏结束语gameover
m_gameOver = CCSprite::create("gameover.png"); m_gameOver->setAnchorPoint(ccp(0.5,0.5)); m_gameOver->setPosition(ccp(0,0)); m_gameOver->setPosition(ccp(size.width/2,size.height/2 + 70)); m_gameOver->setVisible(false); m_gameOver->setScale(0.5); this->addChild(m_gameOver,5);
游戏结束上一步菜单按键
CCMenuItemImage *pCloseItem = CCMenuItemImage::create("back.png","back.png", this,menu_selector(GameMainScene::menuBackCallback) ); pCloseItem->setPosition( ccp(size.width/2,size.height/2 - 50) ); pCloseItem->setScale(0.5); CCMenu* pMenu = CCMenu::create(pCloseItem, NULL); pMenu->setPosition( CCPointZero ); this->addChild(pMenu,5,25); pMenu->setVisible(false); pMenu->setEnabled(false); m_isReduce = false; m_isGameOver = false; scheduleUpdate();
主角及主角子弹
代码:
主角接收触摸
CCDirector* pDirector = CCDirector::sharedDirector(); pDirector->getTouchDispatcher()->addTargetedDelegate(this, 0, true);
主角身体动画
CCAnimation* animation = CCAnimation::create(); animation->addSpriteFrameWithFileName("catBody1.png"); animation->addSpriteFrameWithFileName("catBody2-4.png"); animation->addSpriteFrameWithFileName("catBody3.png"); animation->addSpriteFrameWithFileName("catBody2-4.png"); animation->setDelayPerUnit(0.1f); animation->setRestoreOriginalFrame(true); mainsprite->runAction(CCRepeatForever::create(CCAnimate::create(animation))); this->addChild(mainsprite);
主角尾巴摇摆动画
CCSprite *tail = CCSprite::create("catTail.png"); tail->setAnchorPoint(ccp(0.5,1)); tail->setPosition(ccp(-5,-29)); tail->setScale(0.5); tail->setRotation(20); tail->runAction(CCRepeatForever::create((CCActionInterval*)CCSequence::create(CCRotateBy::create(0.5,-40),CCRotateBy::create(0.5,40),NULL))); this->addChild(tail);
主角手动画
m_leftHand = CCSprite::create("catHand1.png"); m_leftHand->setAnchorPoint(ccp(1,0.5)); m_leftHand->setPosition(ccp(-18,-20)); this->addChild(m_leftHand); m_rightHand = CCSprite::create("catHand2.png"); m_rightHand->setPosition(ccp(18,-20)); m_rightHand->setAnchorPoint(ccp(0,0.5)); this->addChild(m_rightHand); m_offset = ccp(0,0); m_isControl = false;
主角定时发射子弹
schedule(schedule_selector(GameObjHero::releasebullet), 0.8f); void GameObjHero::releasebullet( float dt ) { if(m_isControl)//是否触摸了主角,触摸了则发射子弹 { CCPoint pos = this->getPosition(); //获取主角坐标 GameMainScene *p = (GameMainScene*) this->getParent();//获取父场景调用releaseHeroBullet发射子弹 p->releaseHeroBullet(pos.x,pos.y + 30); } } void GameMainScene::releaseHeroBullet( int x,int y )//參数主角坐标 { for(size_t i = 0;i < m_heroBullets->capacity();i++)//遍历主角每一个子弹 { if(!((GameHeroBullet *)m_heroBullets->objectAtIndex(i))->getIsvisble()) { //设置位置,并设置为显示 ((GameHeroBullet *)m_heroBullets->objectAtIndex(i))->setPosition(ccp(x,y));//子弹起始位置 ((GameHeroBullet *)m_heroBullets->objectAtIndex(i))->setIsVisable();//子弹移动动画 break; } } } void GameHeroBullet::setIsVisable() { this->setVisible(true); m_isVisable = true; int a = (-this->getPosition().y + 550)/150; this->runAction(CCSequence::create(CCMoveTo::create((-this->getPosition().y + 550)/150,ccp(this->getPosition().x,550)),CCCallFuncN::create(this, callfuncN_selector(GameHeroBullet::setIsNotVisable)),NULL));; } void GameHeroBullet::setIsNotVisable(CCNode* pSender) { this->setVisible(false); m_isVisable = false; this->stopAllActions(); }
ccTouchBegan中推断是否点中主角
if(! containsTouchLocation(touch))//没选中主角 { return false; } else//选中了 { m_isControl = true;//发射子弹 //获取触摸偏移 CCPoint touchPoint = touch->getLocationInView(); touchPoint = CCDirector::sharedDirector()->convertToGL(touchPoint); m_offset.x = touchPoint.x - this->getPosition().x; m_offset.y = touchPoint.y - this->getPosition().y; } //选中的点是否在精灵矩形范围内 bool GameObjHero::containsTouchLocation(CCTouch* touch) { return rect().containsPoint(convertTouchToNodeSpaceAR(touch));// } CCRect GameObjHero::rect() { CCSize s = CCSizeMake(85,90); CCPoint p = ccp(-s.width / 2, -s.height / 2); return CCRectMake(-s.width / 2, -s.height / 2, s.width, s.height); }
敌人及敌人子弹
代码:
创建敌人
this->setContentSize(CCSizeMake(99,115)); m_mainBody = CCSprite::create("DrDog1.png"); //敌人自身动画 CCAnimation* animation = CCAnimation::create(); animation->addSpriteFrameWithFileName("DrDog1.png"); animation->addSpriteFrameWithFileName("DrDog2.png"); animation->setDelayPerUnit(0.1f); animation->setRestoreOriginalFrame(true); m_mainBody->runAction(CCRepeatForever::create(CCAnimate::create(animation))); this->addChild(m_mainBody); //初始化爆炸,不可见 m_boom = CCSprite::create("boom1.png"); this->addChild(m_boom); m_boom->setVisible(false); m_isLife = true;
敌人移动动画
int type = CCRANDOM_0_1() * 4;//4种随机移动 //敌人运动轨迹(贝尔曲线) ccBezierConfig bezier2; bezier2.controlPoint_1 = CCPointMake(this->getPosition().x - 400,330); bezier2.controlPoint_2 = CCPointMake(this->getPosition().x + 400,280); bezier2.endPosition = CCPointMake(this->getPosition().x,-70); CCBezierTo * bezierBy2 = CCBezierTo::create(6, bezier2); ccBezierConfig bezier1; bezier1.controlPoint_1 = CCPointMake(this->getPosition().x + 400,330); bezier1.controlPoint_2 = CCPointMake(this->getPosition().x - 400,280); bezier1.endPosition = CCPointMake(this->getPosition().x,-70); CCBezierTo * bezierBy1 = CCBezierTo::create(6, bezier1); switch(type) { case 0: case 1: this->runAction(CCSequence::create(CCMoveBy::create(6,ccp(0,-600)),CCCallFuncN::create(this, callfuncN_selector(GameObjEnemy::reStart)),NULL)); break; case 2: this->runAction(CCSequence::create(bezierBy2,CCCallFuncN::create(this, callfuncN_selector(GameObjEnemy::reStart)),NULL)); break; case 3: this->runAction(CCSequence::create(bezierBy1,CCCallFuncN::create(this, callfuncN_selector(GameObjEnemy::reStart)),NULL)); break; } schedule(schedule_selector(GameObjEnemy::releaseBullet), 1.2f);
当第一波敌人过屏以后,又一次设置一波敌人初始位置及移动的动画, 这样敌人就无限循环出现
void GameObjEnemy::reStart( CCNode* pSender ) { m_mainBody->setVisible(true); m_boom->setVisible(false); CCSize size = CCDirector::sharedDirector()->getWinSize(); this->setPosition(ccp(size.width/4 * m_type,size.height + 50)); m_isLife = true; this->moveStart(); }
每一个敌人发射子弹
void GameObjEnemy::releaseBullet( float dt ) { CCSize size = CCDirector::sharedDirector()->getWinSize(); CCPoint pos = this->getPosition();//敌人位置 if(pos.y > 20 && pos.y < size.height && m_isLife)//敌人出在屏幕上才干发 { GameMainScene *p = (GameMainScene *) this->getParent(); p->releaseEnemyBullet(pos.x,pos.y - 30);//发射子弹 } } void GameMainScene::releaseEnemyBullet( int x,int y ) { for(unsigned int i = 0; i<m_enemyBullets->capacity(); i++)//每一个敌人的子弹10发 { if(!((GameEnemyBullet*)m_enemyBullets->objectAtIndex(i))->getIsvisble()) { ((GameEnemyBullet*)m_enemyBullets->objectAtIndex(i))->setPosition(ccp(x,y)); ((GameEnemyBullet*)m_enemyBullets->objectAtIndex(i))->setIsVisable();//子弹动画 break; } } } void GameEnemyBullet::setIsVisable() { this->setVisible(true); m_isVisable = true; this->runAction(CCRepeatForever::create(CCRotateBy::create(1,360))); this->runAction(CCSequence::create(CCMoveTo::create((this->getPosition().y + 50)/150,ccp(this->getPosition().x,-50)),CCCallFuncN::create(this, callfuncN_selector(GameEnemyBullet::setIsNotVisable)),NULL));; } void GameEnemyBullet::setIsNotVisable(CCNode* pSender) { this->setVisible(false); m_isVisable = false; this->stopAllActions(); }
游戏主页面update定时器进行游戏背景动画处理和主角与敌人攻击碰撞检測
游戏背景动画至上往下移动,当第1张图滑过底部时,第2张图连接上,每次移动2
游戏背景动画至上往下移动,当第1张图滑过底部时,第2张图连接上,每次移动2 m_bg1->setPosition(ccp(m_bg1->getPosition().x,m_bg1->getPosition().y - 2)); m_bg2->setPosition(ccp(m_bg2->getPosition().x,m_bg2->getPosition().y - 2)); if(m_bg2->getPosition().y <= 0) { float temp = m_bg2->getPosition().y + 480; m_bg1->setPosition(ccp(m_bg2->getPosition().x,temp)); } if(m_bg1->getPosition().y <= 0) { float temp = m_bg1->getPosition().y + 480; m_bg2->setPosition(ccp(m_bg1->getPosition().x,temp)); }
三种碰撞检測
敌人和主角、主角子弹碰撞检測
CCPoint hpos = m_hero->getPosition();//主角坐标 for(size_t i = 0;i < m_enemys->capacity();i ++)//遍历敌人 { GameObjEnemy * enemy = ((GameObjEnemy *) m_enemys->objectAtIndex(i));//单个敌人 CCPoint epos = enemy->getPosition(); if(enemy->m_isLife)//子弹击中敌人 { for(size_t i = 0;i < m_heroBullets->capacity();i ++)//主角子弹 { if(((GameHeroBullet *)m_heroBullets->objectAtIndex(i))->getIsvisble()) { if(isHit(((GameHeroBullet *)m_heroBullets->objectAtIndex(i))->getPosition(),epos,5,13,21,28))//主角子弹打中敌人碰撞检測 { enemy->setDie();//敌人消失,有爆炸动画 m_gameMark->addNumber(200);//加分 break; } } } } //敌人和主角碰撞 if(!m_isReduce && enemy->m_isLife && isHit(hpos,epos,21,22.5,21,28)) { enemy->setDie(); setHeroHurt();//主角掉命 } }
主角被敌人子弹打中碰撞检測
if(!m_isReduce) { for(size_t i = 0;i < m_enemyBullets->capacity();i ++)//敌人的子弹10发 { if(isHit(hpos,((GameEnemyBullet *)m_enemyBullets->objectAtIndex(i))->getPosition(),21,22.5,5,13)) { setHeroHurt();//主角掉命 } } }
推断两个矩形是否碰撞
bool GameMainScene::isHit( CCPoint p1,CCPoint p2,int w1,int h1,int w2,int h2 ) { if(abs(p1.x - p2.x) < w1 + w2 && abs(p1.y - p2.y) < h1 + h2) { return true; } return false; }
主角掉命
void GameMainScene::setHeroHurt() { m_hero->stopAllActions(); switch(m_blood)//初始3 { case 3: m_blood1->setVisible(false);//命图标不可见 m_blood --; break; case 2: m_blood2->setVisible(false); m_blood --; break; case 1: m_blood3->setVisible(false); m_blood --; break; case 0: if(! m_isGameOver) { m_isGameOver = true; setGameOver();//3条命用完,游戏结束 } break; } CCActionInterval* action = CCBlink::create(5, 10);//3秒内闪烁6次 m_hero->runAction(action); scheduleOnce(schedule_selector(GameMainScene::resetReduce), 5.0f);//5秒后运行一次 m_isReduce = true;//5秒不受伤害 } void GameMainScene::resetReduce( float dt ) { m_isReduce = false;//又一次能收到伤害 }
敌人消失后爆炸
void GameObjEnemy::setDie() { //敌人消失 m_isLife = false; m_mainBody->setVisible(false); m_boom->setVisible(true); this->stopAllActions(); //爆炸动画 CCAnimation* boomAnimation = CCAnimation::create(); boomAnimation->addSpriteFrameWithFileName("boom1.png"); boomAnimation->addSpriteFrameWithFileName("boom2.png"); boomAnimation->addSpriteFrameWithFileName("boom3.png"); boomAnimation->addSpriteFrameWithFileName("boom4.png"); boomAnimation->addSpriteFrameWithFileName("boom5.png"); boomAnimation->setDelayPerUnit(0.1f); boomAnimation->setRestoreOriginalFrame(true); m_boom->runAction(CCSequence::create(CCAnimate::create(boomAnimation),CCCallFuncN::create(this, callfuncN_selector(GameObjEnemy::reStart)),NULL)); }
竖屏小游戏--喵星战争源代码分析【完整】,布布扣,bubuko.com
原文:http://www.cnblogs.com/mfrbuaa/p/3813729.html