cocos2d-x学习日志(10) --射击游戏(喵星战争)
2013-08-30 18:38
405 查看
转载请标明:转载自【小枫栏目】,博文链接:http://blog.csdn.net/rexuefengye/article/details/10553487
滚动背景、主角、敌人、子弹、特效
该游戏采用移动游戏中流行的生存模式,即没有关卡的限制,主角有三次失败(撞上狗博士或管子弹)的机会,游戏结束。
主菜单和关于界面,如图
主游戏界面和游戏结束界面
GameObjHero.h
GameObjHero.cpp
3.2 敌人狗博士
GameObjEnemy.h
GameObjEnemy.cpp
3.3 鱼骨子弹
GameHeroBullet.h
GameHeroBullet.cpp
3.4 试管子弹
GameEnemyBullet.h
GameEnemyBullet.cpp
3.5 游戏分数实现
GameMark.h
GameMark.cpp
3.6 游戏主模块的实现
GameScene.h
GameScene.cpp
3.7 主菜单
GameMenuScene.h
GameMenuScene.cpp
3.8 关于界面的实现
GameAboutScene.h
GameAboutScene.cpp
到此结束了!
参考书籍《Cocos2d-x 权威指南》
Demo下载地址:http://download.csdn.net/detail/my183100521/6036929
一、纵版射击游戏的特点
纵版射击游戏是一种比较传统的游戏,在各种游戏平台都有非常经典的游戏作品。对于游戏开发者来说,这种游戏题材非常适合加入特效和创新的玩法。但是无论怎样改变,该类游戏都具备以下特点:滚动背景、主角、敌人、子弹、特效
二、喵星战争简介
喵星战争在传统纵版设计游戏的基础上进行创新。主角不是传统飞机造型,而是一只小猫,敌人也不是敌机与怪兽,而是狗博士,同时子弹也做了相应变化,如图1) 游戏规则:
喵星战争的游戏规则:用手指控制主角小猫的移动,当小猫处于被控制状态时,会放出子弹,狗博士不断地从屏幕上部出现,玩家要做就是移动小猫主角,使小猫的子弹打中狗博士,打中狗博士可以增加积分。该游戏采用移动游戏中流行的生存模式,即没有关卡的限制,主角有三次失败(撞上狗博士或管子弹)的机会,游戏结束。
2)游戏框架和界面
喵星战争界面和流程较简单,包括主菜单、关于、游戏过程等界面。主菜单和关于界面,如图
主游戏界面和游戏结束界面
3)实现代码
3.1 主角小猫类:GameObjHero.h
#ifndef example15_1_GameObjHero_h #define example15_1_GameObjHero_h #include "cocos2d.h" using namespace cocos2d; class GameObjHero : public CCNode, public CCTargetedTouchDelegate { public: CCSprite*lefthand; //左手 CCSprite*righthand; //右手 CCPoint offset; //触摸偏移位置 bool iscontrol; //是否在控制主角 GameObjHero(void); virtual ~GameObjHero(void); void releasebullet(); //释放子弹 CCRect rect(); virtual void onEnter(); virtual void onExit(); bool containsTouchLocation(CCTouch* touch); virtual bool ccTouchBegan(CCTouch* touch, CCEvent* event); virtual void ccTouchMoved(CCTouch* touch, CCEvent* event); virtual void ccTouchEnded(CCTouch* touch, CCEvent* event); virtual void touchDelegateRetain(); virtual void touchDelegateRelease(); }; #endif
GameObjHero.cpp
#include "GameObjHero.h" #include "GameScene.h" #include "GameHeroBullet.h" GameObjHero::GameObjHero(void) { } GameObjHero::~GameObjHero(void) { } CCRect GameObjHero::rect() { CCSize s = CCSizeMake(85,90); return CCRectMake(-s.width / 2, -s.height / 2, s.width, s.height); } void GameObjHero::touchDelegateRetain() { this->retain(); } void GameObjHero::touchDelegateRelease() { this->release(); } void GameObjHero::onEnter() { CCNode::onEnter(); this->setContentSize(CCSizeMake(85,90)); CCDirector* pDirector = CCDirector::sharedDirector(); pDirector->getTouchDispatcher()->addTargetedDelegate(this, 0, true); CCSprite *mainsprite = CCSprite::create("catBody1.png"); //主体动画 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))); 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))); addChild(tail); //手 lefthand = CCSprite::create("catHand1.png"); lefthand->setAnchorPoint(ccp(1,0.5)); lefthand->setPosition(ccp(-18,-20)); addChild(lefthand); righthand = CCSprite::create("catHand2.png"); righthand->setPosition(ccp(18,-20)); righthand->setAnchorPoint(ccp(0,0.5)); addChild(righthand); offset = ccp(0,0); iscontrol = false; schedule(schedule_selector(GameObjHero::releasebullet), 1.0f); } void GameObjHero::releasebullet(){ //释放子弹 if(iscontrol){ CCPoint pos = this->getPosition(); GameMain *p = (GameMain *) this->getParent(); p->releaseheroBullet(pos.x,pos.y + 30); } } void GameObjHero::onExit() { CCDirector* pDirector = CCDirector::sharedDirector(); pDirector->getTouchDispatcher()->removeDelegate(this); CCNode::onExit(); } bool GameObjHero::containsTouchLocation(CCTouch* touch) { return CCRect::CCRectContainsPoint(rect(), convertTouchToNodeSpaceAR(touch)); } bool GameObjHero::ccTouchBegan(CCTouch* touch, CCEvent* event) { if(((GameMain *)this->getParent())->isover) return false; if(! containsTouchLocation(touch)){ return false; }else{ //获得触摸偏移位置 iscontrol = true; CCPoint touchPoint = touch->locationInView(); touchPoint = CCDirector::sharedDirector()->convertToGL(touchPoint); offset.x = touchPoint.x - this->getPosition().x; offset.y = touchPoint.y - this->getPosition().y; } return true; } void GameObjHero::ccTouchMoved(CCTouch* touch, CCEvent* event) { if(iscontrol){ CCPoint touchPoint = touch->locationInView(); touchPoint = CCDirector::sharedDirector()->convertToGL(touchPoint); //设置左右手上下 float x = touchPoint.x - offset.x; float y = touchPoint.y - offset.y; if(x < this->getPosition().x){ lefthand->setFlipY(true); righthand->setFlipY(false); }else{ lefthand->setFlipY(false); righthand->setFlipY(true); } this->setPosition(x,y); } } void GameObjHero::ccTouchEnded(CCTouch* touch, CCEvent* event) { if(iscontrol){ iscontrol = false; lefthand->setFlipY(false); righthand->setFlipY(false); } }
3.2 敌人狗博士
GameObjEnemy.h
#ifndef example15_1_GameObjEnemy_h #define example15_1_GameObjEnemy_h #include "cocos2d.h" using namespace cocos2d; class GameObjEnemy : public CCNode { public: CCSprite *boom; CCSprite *mainbody; GameObjEnemy(void); virtual ~GameObjEnemy(void); void releasebullet(); virtual void onEnter(); virtual void onExit(); void movestart(); void restart(); void setdie(); short type; bool islife; void setType(short var); }; #endif
GameObjEnemy.cpp
#include "GameObjEnemy.h" #include "GameScene.h" GameObjEnemy::GameObjEnemy(void) { } GameObjEnemy::~GameObjEnemy(void) { } void GameObjEnemy::onEnter() { CCNode::onEnter(); this->setContentSize(CCSizeMake(99,111)); mainbody = CCSprite::create("DrDog1.png"); //初始化动画 CCAnimation* animation = CCAnimation::create(); animation->addSpriteFrameWithFileName("DrDog1.png"); animation->addSpriteFrameWithFileName("DrDog2.png"); animation->setDelayPerUnit(0.1f); animation->setRestoreOriginalFrame(true); mainbody->runAction(CCRepeatForever::create(CCAnimate::create(animation))); addChild(mainbody); boom = CCSprite::create("boom1.png"); addChild(boom); boom->setVisible(false); islife = true; } void GameObjEnemy::setdie(){ islife = false; mainbody->setVisible(false); 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); boom->runAction(CCSequence::create(CCAnimate::create(boomAnimation),CCCallFuncN::create(this, callfuncN_selector(GameObjEnemy::restart)),NULL)); } void GameObjEnemy::releasebullet(){ CCSize size = CCDirector::sharedDirector()->getWinSize(); CCPoint pos = this->getPosition(); if(pos.y > 20 && pos.y < size.height && islife){ GameMain *p = (GameMain *) this->getParent(); p->releaseenemyBullet(pos.x,pos.y - 30); } } void GameObjEnemy::onExit() { CCNode::onExit(); } void GameObjEnemy::setType(short var){ type = var; } void GameObjEnemy::restart(){ mainbody->setVisible(true); boom->setVisible(false); CCSize size = CCDirector::sharedDirector()->getWinSize(); this->setPosition(ccp(size.width/4 * type,size.height + 50)); islife = true; mainbody->setVisible(true); boom->setVisible(false); this->movestart(); } void GameObjEnemy::movestart(){ islife = true; int type = CCRANDOM_0_1() * 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); }
3.3 鱼骨子弹
GameHeroBullet.h
#ifndef example15_1_GameHeroBullet_h #define example15_1_GameHeroBullet_h #include "cocos2d.h" using namespace cocos2d; class GameHeroBullet : public CCNode { public: bool isvisable; GameHeroBullet(void); virtual ~GameHeroBullet(void); void setIsVisable(); void setIsNotVisable(); bool getIsvisble(); virtual void onEnter(); virtual void onExit(); }; #endif
GameHeroBullet.cpp
#include "GameHeroBullet.h" GameHeroBullet::GameHeroBullet(void) { isvisable = false; } GameHeroBullet::~GameHeroBullet(void) { } void GameHeroBullet::setIsVisable(){ //子弹移动,运行动作 this->setVisible(true); isvisable = true; 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(){ this->setVisible(false); isvisable = false; this->stopAllActions(); } bool GameHeroBullet::getIsvisble(){ return isvisable; } void GameHeroBullet::onEnter() { CCNode::onEnter(); //初始化主体 this->setContentSize(CCSizeMake(21,52)); CCSprite *mainbody = CCSprite::create("YuGuZD.png"); addChild(mainbody); } void GameHeroBullet::onExit() { CCNode::onExit(); }
3.4 试管子弹
GameEnemyBullet.h
#ifndef example15_1_GameEnemyBullet_h #define example15_1_GameEnemyBullet_h #include "cocos2d.h" using namespace cocos2d; class GameEnemyBullet : public CCNode { public: bool isvisable; GameEnemyBullet(void); virtual ~GameEnemyBullet(void); void setIsVisable(); void setIsNotVisable(); bool getIsvisble(); virtual void onEnter(); virtual void onExit(); }; #endif
GameEnemyBullet.cpp
#include "GameEnemyBullet.h" GameEnemyBullet::GameEnemyBullet(void) { isvisable = false; } GameEnemyBullet::~GameEnemyBullet(void) { } void GameEnemyBullet::onEnter() { CCNode::onEnter(); //初始化主体 this->setContentSize(CCSizeMake(21,52)); CCSprite *mainbody = CCSprite::create("DrDogZD.png"); mainbody->runAction(CCRepeatForever::create(CCRotateBy::create(1,360))); addChild(mainbody); } bool GameEnemyBullet::getIsvisble(){ return isvisable; } void GameEnemyBullet::onExit() { CCNode::onExit(); } void GameEnemyBullet::setIsVisable(){ //运行动作 this->setVisible(true); 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(){ this->setVisible(false); isvisable = false; this->stopAllActions(); }
3.5 游戏分数实现
GameMark.h
#ifndef example15_1_GameMark_h #define example15_1_GameMark_h #include "cocos2d.h" using namespace cocos2d; class GameMark : public CCNode { public: GameMark(void); virtual ~GameMark(void); virtual void onEnter(); virtual void onExit(); CCArray *bits; int mark; void addnumber(int var); CCTexture2D* ui; }; #endif
GameMark.cpp
#include "GameMark.h" GameMark::GameMark(void) { } GameMark::~GameMark(void) { } void GameMark::onExit() { CCNode::onExit(); } void GameMark::onEnter() { CCNode::onEnter(); CCSize size = CCDirector::sharedDirector()->getWinSize(); this->setContentSize(size); bits = CCArray::create(); //分数标题 CCSprite *title = CCSprite::create("score.png"); title->setPosition(ccp(size.width/2 + 40,size.height - 15)); title->setScale(0.5); addChild(title); //数字按位设置 for(int i = 0;i < 5;i ++){ CCSprite * shu = CCSprite::create("shu.png"); ui = shu->getTexture(); shu->setScale(0.5); shu->setTextureRect(CCRectMake(234,0,26,31)); shu->setPosition(ccp(size.width - 15 - i * 15,size.height - 15)); bits->addObject(shu); addChild(shu); } bits->retain(); mark = 0; } void GameMark::addnumber(int var){ //分数,按位设置数字 mark += var; int temp = mark % 10; if(temp > 0){ ((CCSprite *)bits->objectAtIndex(0))->setTexture(ui); ((CCSprite *)bits->objectAtIndex(0))->setTextureRect(CCRectMake((temp - 1) * 26,0,26,31)); }else{ ((CCSprite *)bits->objectAtIndex(0))->setTexture(ui); ((CCSprite *)bits->objectAtIndex(0))->setTextureRect(CCRectMake(234,0,26,31)); } temp = (mark % 100) / 10; if(temp > 0){ ((CCSprite *)bits->objectAtIndex(0))->setTexture(ui); ((CCSprite *)bits->objectAtIndex(1))->setTextureRect(CCRectMake((temp - 1) * 26,0,26,31)); }else{ ((CCSprite *)bits->objectAtIndex(0))->setTexture(ui); ((CCSprite *)bits->objectAtIndex(1))->setTextureRect(CCRectMake(234,0,26,31)); } temp = (mark % 1000) / 100; if(temp > 0){ ((CCSprite *)bits->objectAtIndex(0))->setTexture(ui); ((CCSprite *)bits->objectAtIndex(2))->setTextureRect(CCRectMake((temp - 1) * 26,0,26,31)); }else{ ((CCSprite *)bits->objectAtIndex(0))->setTexture(ui); ((CCSprite *)bits->objectAtIndex(2))->setTextureRect(CCRectMake(234,0,26,31)); } temp = (mark % 10000) / 1000; if(temp > 0){ ((CCSprite *)bits->objectAtIndex(0))->setTexture(ui); ((CCSprite *)bits->objectAtIndex(3))->setTextureRect(CCRectMake((temp - 1) * 26,0,26,31)); }else{ ((CCSprite *)bits->objectAtIndex(0))->setTexture(ui); ((CCSprite *)bits->objectAtIndex(3))->setTextureRect(CCRectMake(234,0,26,31)); } temp = mark / 10000; if(temp > 0){ ((CCSprite *)bits->objectAtIndex(0))->setTexture(ui); ((CCSprite *)bits->objectAtIndex(4))->setTextureRect(CCRectMake((temp - 1) * 26,0,26,31)); }else{ ((CCSprite *)bits->objectAtIndex(0))->setTexture(ui); ((CCSprite *)bits->objectAtIndex(4))->setTextureRect(CCRectMake(234,0,26,31)); } }
3.6 游戏主模块的实现
GameScene.h
#ifndef example15_1_GameScene_h #define example15_1_GameScene_h #include "cocos2d.h" #include "GameObjHero.h" #include "GameObjEnemy.h" #include "GameMark.h" using namespace cocos2d; class GameMain : public cocos2d::CCLayer { public: static GameMain sGameMain; CCSprite *blood1; CCSprite *blood2; CCSprite *blood3; CCSprite *bg1; CCSprite *bg2; short blood; CCArray *bullets; CCArray *enemybullets; void menuBackCallback(); bool isreduce; bool isover; GameObjHero *hero; void setover(); CCSprite* gameover; CCArray *enemys; virtual bool init(); virtual void update(float time); static cocos2d::CCScene* scene(); void releaseenemyBullet(int x,int y); void releaseheroBullet(int x,int y); bool isCollion(CCPoint p1,CCPoint p2,int w1,int h1,int w2,int h2); void setherohurt(); void resetreduce(); GameMark* gamemark; CREATE_FUNC(GameMain); }; #endif
GameScene.cpp
#include "GameAboutScene.h" #include "GameMenuScene.h" #include "GameScene.h" #include "GameObjHero.h" #include "GameHeroBullet.h" #include "GameEnemyBullet.h" using namespace cocos2d; CCScene* GameMain::scene() { CCScene *scene = CCScene::create(); GameMain *layer = GameMain::create(); scene->addChild(layer); return scene; } void GameMain::releaseenemyBullet(int x,int y){ //遍历子弹数组,不在使用的子弹释放 for(int i = 0;i < enemybullets->capacity();i ++){ if(!((GameEnemyBullet *)enemybullets->objectAtIndex(i))->getIsvisble()){ //设置位置,并设置为显示 ((GameEnemyBullet *)enemybullets->objectAtIndex(i))->setPosition(ccp(x,y)); ((GameEnemyBullet *)enemybullets->objectAtIndex(i))->setIsVisable(); break; } } } void GameMain::releaseheroBullet(int x,int y){ //遍历子弹数组,不在使用的子弹释放 for(int i = 0;i < bullets->capacity();i ++){ if(!((GameHeroBullet *)bullets->objectAtIndex(i))->getIsvisble()){ //设置位置,并设置为显示 ((GameHeroBullet *)bullets->objectAtIndex(i))->setPosition(ccp(x,y)); ((GameHeroBullet *)bullets->objectAtIndex(i))->setIsVisable(); break; } } } bool GameMain::init() { if ( !CCLayer::init() ) { return false; } CCSize size = CCDirector::sharedDirector()->getWinSize(); //创建背景 bg1 = CCSprite::create("bg.png"); bg1->setScale(0.5); bg2 = CCSprite::create("bg.png"); bg2->setScale(0.5); bg1->setAnchorPoint(ccp(0,0)); bg2->setAnchorPoint(ccp(0,0)); bg1->setPosition( ccp(0,0) ); bg2->setPosition( ccp(0,size.height) ); this->addChild(bg1, 0); this->addChild(bg2, 0); //创建主角 hero = new GameObjHero(); hero->setPosition(ccp(size.width/2,-50)); hero->setScale(0.5); addChild(hero,2,1); hero->runAction(CCMoveBy::create(0.5,ccp(0,150))); //创建敌人 enemys = CCArray::create(); 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); enemys->addObject(enemy); addChild(enemy,1); enemy->movestart(); } enemys->retain(); //创建血量ui blood = 3; CCSpriteBatchNode* ui = CCSpriteBatchNode::create("cat.png"); //CCNode *ui = CCNode::create(); // blood1 = CCSprite::create(ui->getTexture()); blood1 = CCSprite::createWithTexture(ui->getTexture()); blood1->setPosition(ccp(20,size.height - 20)); blood1->setScale(0.2); ui->addChild(blood1); blood2 = CCSprite::createWithTexture(ui->getTexture()); blood2->setPosition(ccp(50,size.height - 20)); blood2->setScale(0.2); ui->addChild(blood2); blood3 = CCSprite::createWithTexture(ui->getTexture()); blood3->setPosition(ccp(80,size.height - 20)); blood3->setScale(0.2); ui->addChild(blood3); addChild(ui,4); //初始化主角子弹 bullets = CCArray::create(); for(int i = 0;i < bullets->capacity();i ++){ GameHeroBullet * mybullet = new GameHeroBullet(); mybullet->setIsNotVisable(); mybullet->setScale(0.5); bullets->addObject(mybullet); this->addChild(mybullet,3); } //初始化敌人子弹 bullets->retain(); enemybullets = CCArray::create(); for(int i = 0;i < enemybullets->capacity();i ++){ GameEnemyBullet * mybullet = new GameEnemyBullet(); mybullet->setIsNotVisable(); mybullet->setScale(0.5); enemybullets->addObject(mybullet); this->addChild(mybullet,3); } gamemark = new GameMark(); addChild(gamemark,4); enemybullets->retain(); //初始化游戏结束弹板及按钮 gameover = CCSprite::create("gameover.png"); gameover->setAnchorPoint(ccp(0.5,0.5)); gameover->setPosition(ccp(0,0)); gameover->setPosition(ccp(size.width/2,size.height/2 + 70)); gameover->setVisible(false); gameover->setScale(0.5); addChild(gameover,5); CCMenuItemImage *pCloseItem = CCMenuItemImage::create("back.png","back.png", this,menu_selector(GameMain::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); isreduce = false; isover = false; scheduleUpdate(); return true; } void GameMain::menuBackCallback(){ CCDirector::sharedDirector()->replaceScene(GameMenu::scene()); } bool GameMain::isCollion(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 GameMain::setover(){ //设置游戏结束 CCMenu* pMenu = (CCMenu *)this->getChildByTag(25); pMenu->setVisible(true); pMenu->setEnabled(true); gameover->setVisible(true); gameover->setScale(0); pMenu->setScale(0); pMenu->runAction(CCScaleTo::create(0.5,1)); gameover->runAction(CCScaleTo::create(0.5,0.5)); } void GameMain::setherohurt(){ //主角受伤,减血 hero->stopAllActions(); switch(blood){ case 3: blood1->setVisible(false); blood --; break; case 2: blood2->setVisible(false); blood --; break; case 1: blood3->setVisible(false); blood --; break; case 0: if(! isover){ isover = true; setover(); } break; } CCActionInterval* action = CCBlink::create(5, 10); hero->runAction(action); schedule(schedule_selector(GameMain::resetreduce), 5.0f); isreduce = true; } void GameMain::resetreduce(){ isreduce = false; } void GameMain::update(float time){ //背景移动逻辑 bg1->setPosition(ccp(bg1->getPosition().x,bg1->getPosition().y - 2)); bg2->setPosition(ccp(bg2->getPosition().x,bg2->getPosition().y - 2)); if(bg2->getPosition().y < 0){ float temp = bg2->getPosition().y + 480; bg1->setPosition(ccp(bg2->getPosition().x,temp)); } if(bg1->getPosition().y < 0){ float temp = bg1->getPosition().y + 480; bg2->setPosition(ccp(bg1->getPosition().x,temp)); } CCPoint hpos = hero->getPosition(); //敌人和子弹碰撞检测 for(int i = 0;i < 3;i ++){ GameObjEnemy * enemy = ((GameObjEnemy *) enemys->objectAtIndex(i)); CCPoint epos = enemy->getPosition(); if(enemy->islife){ for(int i = 0;i < bullets->capacity();i ++){ if(((GameHeroBullet *)bullets->objectAtIndex(i))->getIsvisble()){ if(isCollion(((GameHeroBullet *)bullets->objectAtIndex(i))->getPosition(),epos,5,13,21,28)){ enemy->setdie(); gamemark->addnumber(200); break; } } } } if(!isreduce && enemy->islife && isCollion(hpos,epos,21,22.5,21,28)){ enemy->setdie(); setherohurt(); } } //主角和敌人子弹碰撞 if(!isreduce){ for(int i = 0;i < enemybullets->capacity();i ++){ if(isCollion(hpos,((GameEnemyBullet *)enemybullets->objectAtIndex(i))->getPosition(),21,22.5,5,13)){ setherohurt(); } } } }
3.7 主菜单
GameMenuScene.h
#ifndef example15_1_GameMenuScene_h #define example15_1_GameMenuScene_h #include "cocos2d.h" using namespace cocos2d; class GameMenu : public cocos2d::CCLayer { public: bool issound; CCMenuItemImage *soundItem; virtual bool init(); virtual void onEnter(); virtual void onExit(); static cocos2d::CCScene* scene(); void menuEnter(); void menuNewGameCallback(CCObject* pSender); void menuContinueCallback(CCObject* pSender); void menuAboutCallback(CCObject* pSender); void menuSoundCallback(CCObject* pSender); CREATE_FUNC(GameMenu); }; #endif
GameMenuScene.cpp
#include "GameMenuScene.h" #include "GameAboutScene.h" #include "GameScene.h" #include "SimpleAudioEngine.h" using namespace cocos2d; using namespace CocosDenshion; CCScene* GameMenu::scene() { CCScene *scene = CCScene::create(); GameMenu *layer = GameMenu::create(); scene->addChild(layer); return scene; } bool GameMenu::init() { if ( !CCLayer::init() ) { return false; } CCSize size = CCDirector::sharedDirector()->getWinSize(); //初始化背景 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 * 2, 0)); this->addChild(bgstar,1,1); //初始化标题 CCNode *title = CCNode::create(); title->setContentSize(CCSizeMake(size.width - 40,50)); 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(GameMenu::menuNewGameCallback)); 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(GameMenu::menuContinueCallback)); 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(GameMenu::menuAboutCallback)); aboutItem->setScale(0.5); aboutItem->setPosition(ccp(size.width / 2,size.height / 2 - 140)); aboutItem->setEnabled(false); soundItem = CCMenuItemImage::create("sound-on-A.png", "sound-on-B.png",this,menu_selector(GameMenu::menuSoundCallback)); soundItem->setScale(0.5); soundItem->setEnabled(false); soundItem->setPosition(ccp(40,40)); //使用按钮创建菜单 CCMenu* mainmenu = CCMenu::create(newGameItem,continueItem,aboutItem,soundItem,NULL); mainmenu->setPosition(ccp(0,0)); this->addChild(mainmenu,1,3); //初始化声音部分 issound = true;//是否开启声音参数 SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic( CCFileUtils::sharedFileUtils()->fullPathFromRelativePath("background.mp3") ); SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(0.5); SimpleAudioEngine::sharedEngine()->playBackgroundMusic(std::string(CCFileUtils::sharedFileUtils()->fullPathFromRelativePath("background.mp3")).c_str(), true); return true; } void GameMenu::onEnter(){ CCLayer::onEnter(); CCSize size = CCDirector::sharedDirector()->getWinSize(); //界面进入时,运行菜单项进入动作 CCNode* mainmenu = this->getChildByTag(3); mainmenu->setPositionX(-200); mainmenu->runAction(CCSequence::create(CCEaseElasticIn::create(CCMoveTo::create(0.5,ccp(0,0))),CCCallFuncN::create(this, callfuncN_selector(GameMenu::menuEnter)),NULL)); CCNode*title = this->getChildByTag(2); title->setPositionY(size.height + 20); title->runAction(CCEaseElasticIn::create(CCMoveBy::create(0.5,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 GameMenu::menuEnter(){ //菜单进入后,菜单项点击有效 CCNode* mainmenu = this->getChildByTag(3); CCArray* temp = mainmenu->getChildren(); for(int i = 0;i < temp->count();i ++) ((CCMenuItemImage *)temp->objectAtIndex(i))->setEnabled(true); } void GameMenu::onExit(){ CCLayer::onExit(); } void GameMenu::menuNewGameCallback(CCObject* pSender) { CCDirector::sharedDirector()->replaceScene(GameMain::scene()); } void GameMenu::menuContinueCallback(CCObject* pSender) { CCDirector::sharedDirector()->replaceScene(GameMain::scene()); } void GameMenu::menuAboutCallback(CCObject* pSender) { CCDirector::sharedDirector()->replaceScene(GameAbout::scene()); } void GameMenu::menuSoundCallback(CCObject* pSender) { if(! issound){ soundItem->setNormalImage(CCSprite::create("sound-on-A.png")); soundItem->setDisabledImage(CCSprite::create("sound-on-B.png")); SimpleAudioEngine::sharedEngine()->playBackgroundMusic(std::string(CCFileUtils::sharedFileUtils()->fullPathFromRelativePath("background.mp3")).c_str(), true); issound = true; }else{ soundItem->setNormalImage(CCSprite::create("sound-off-A.png")); soundItem->setDisabledImage(CCSprite::create("sound-off-B.png")); SimpleAudioEngine::sharedEngine()->stopBackgroundMusic(); issound = false; } }
3.8 关于界面的实现
GameAboutScene.h
#ifndef example15_1_GameAboutScene_h #define example15_1_GameAboutScene_h #include "cocos2d.h" using namespace cocos2d; class GameAbout : public cocos2d::CCLayer { public: virtual bool init(); virtual void onEnter(); virtual void onExit(); static cocos2d::CCScene* scene(); void menuBackCallback(CCObject* pSender); void menuEnter(); CREATE_FUNC(GameAbout); }; #endif
GameAboutScene.cpp
#include "GameAboutScene.h" #include "GameMenuScene.h" using namespace cocos2d; CCScene* GameAbout::scene() { CCScene *scene = CCScene::create(); GameAbout *layer = GameAbout::create(); scene->addChild(layer); return scene; } bool GameAbout::init() { if ( !CCLayer::init() ) { return false; } CCSize size = CCDirector::sharedDirector()->getWinSize(); //初始化背景 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,"name:meow war\n\nprogram:shuoquan man\n\nart design:peng xu\n\ncompany:hz books\n\n powered by cocos2D-x"); // CCLabelTTF * myjineng = CCLabelTTF::create(inf,CCSizeMake(400,400),kCCTextAlignmentLeft, "Marker Felt", 40); CCLabelTTF * myjineng = CCLabelTTF::create(inf, "Marker Felt", 40, CCSizeMake(400,400), kCCTextAlignmentLeft); myjineng->setAnchorPoint(ccp(0,1)); myjineng->setColor(ccc3(200,200,200)); myjineng->setPosition(ccp(50,600)); kuang->addChild(myjineng); //初始化关于标题 CCSprite*abouttitle = CCSprite::create("about.png"); abouttitle->setScale(0.5); abouttitle->setPosition(ccp(size.width/2, size.height - 20)); this->addChild(abouttitle,3,3); //初始化返回按钮 CCMenuItemImage *back = CCMenuItemImage::create("backA.png", "backB.png",this,menu_selector(GameAbout::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); return true; } void GameAbout::menuBackCallback(CCObject* pSender){ CCDirector::sharedDirector()->replaceScene(GameMenu::scene()); } void GameAbout::menuEnter(){ //菜单进入后,菜单项点击有效 CCNode* mainmenu = this->getChildByTag(4); CCArray* temp = mainmenu->getChildren(); ((CCMenuItemImage *)temp->objectAtIndex(0))->setEnabled(true); } void GameAbout::onExit(){ CCLayer::onExit(); } void GameAbout::onEnter(){ CCLayer::onEnter(); //界面进入时,运行菜单项进入动作 CCSize size = CCDirector::sharedDirector()->getWinSize(); CCNode* mainmenu = this->getChildByTag(4); mainmenu->setPositionX(-100); mainmenu->runAction(CCSequence::create(CCEaseElasticIn::create(CCMoveBy::create(0.5,ccp(100,0))),CCCallFuncN::create(this, callfuncN_selector(GameAbout::menuEnter)),NULL)); //加速度动作 CCNode*title = this->getChildByTag(3); title->setPositionY(size.height + 20); title->runAction(CCEaseElasticIn::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(CCEaseElasticIn::create(CCMoveTo::create(0.5,ccp(size.width/2,size.height/2)))); }
到此结束了!
参考书籍《Cocos2d-x 权威指南》
Demo下载地址:http://download.csdn.net/detail/my183100521/6036929
相关文章推荐
- cocos2d-x学习日志(10) --射击游戏(喵星战争)
- cocos2dx - 制作纵版射击游戏:喵星战争 (三)
- Cocos2d-x 3.1.1 学习日志9--一“上一下其乐无穷”游戏开发系列一
- cocos2dx - 制作纵版射击游戏:喵星战争 (一)
- cocos2dx - 制作纵版射击游戏:喵星战争 (七)
- cocos2dx - 制作纵版射击游戏:喵星战争 (二)
- cocos2dx - 制作纵版射击游戏:喵星战争 (五)
- cocos2d-x学习(3)---游戏实例开发:一个简单的射击游戏二
- cocos2d-x学习(4)---游戏实例开发:一个简单的射击游戏三
- Cocos2d-x 3.1.1 学习日志9--一“上一下其乐无穷”游戏开发系列一
- cocos2dx - 制作纵版射击游戏:喵星战争 (四)
- cocos2d-x学习(2)---游戏实例开发:一个简单的射击游戏一
- cocos2dx - 制作纵版射击游戏:喵星战争 (六)
- 【麦可网】Cocos2d-X跨平台游戏开发学习笔记---第二十五课:Cocos2D-X物理引擎之Box2D8-10
- LayaAir引擎学习日志10----LayaAir的一些游戏演示和游戏项目源码开源
- 【Cocos2d-x3.0学习笔记 10】游戏实例跑跑跑02
- Cocos2d-x 3.1.1 学习日志10--一进来你就知道了Box2D了
- Cocos2d-x 3.1.1 学习日志10--一进来你就知道了Box2D了
- 【Cocos2d-html5游戏引擎学习笔记(10)】自定义精灵动画
- cocos2d-x学习日志(9) --战争烟雾