cocos2d-x 2.x打飞机(一)

首先创建Cocos2d-x项目,命名为Plane,在AppDelegate类中的applicationDidFinishLaunching方法中增加一行屏幕适配的代码。

然后创建菜单层MenuLayer,继承至CCLayerColor类(带颜色的层),MenuLayer类中有如下方法:

static CCScene* scene();

CREATE_FUNC(MenuLayer);

bool init();

void addXXX();

void addBackGround();

void addLogo();

void addMenu();

void onStarGame(CCObject* sender)。

首先实现场景创建方法scene,该方法中创建场景并添加菜单层。

接下来实现init方法,该方法添加欢迎界面,欢迎界面是一个logo的显示和消失


最后实现菜单层界面上的精灵、菜单项和菜单。

然后创建一个地图层MapLayer,继承至CCLayer,该层用于创建游戏的背景地图,游戏滚动地图的实现由两个地图背景精灵交替滚动实现,该类有以下方法和属性:

static CCScene* scene(int Level);

static MapLayer* createMapLayer(int Level);

CCSprite *m_map1;

CCSprite *m_map2;

int m_level;

bool initMapLayer(int Level);

void initMap();

void mapMove(float time);

1.3 步骤

实现此案例需要按照如下步骤进行。

步骤一:创建菜单层MenuLayer

首先创建Cocos2d-x项目,命名为Plane,在AppDelegate类中的applicationDidFinishLaunching方法中增加一行屏幕适配的代码,代码如下所示:

    
    
    
    
  1. bool AppDelegate::applicationDidFinishLaunching() {
  2. // initialize director
  3. CCDirector* pDirector = CCDirector::sharedDirector();
  4. CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();
  5. pDirector->setOpenGLView(pEGLView);
  6. //屏幕适配
  7. pEGLView->setDesignResolutionSize(320, 568, kResolutionExactFit);
  8. pDirector->setDisplayStats(true);
  9. pDirector->setAnimationInterval(1.0 / 60);
  10. CCScene *pScene = MenuLayer::scene();
  11. pDirector->runWithScene(pScene);
  12. return true;
  13. }

然后创建菜单层MenuLayer,继承至CCLayerColor类(带颜色的层),代码如下所示:

    
    
    
    
  1. class MenuLayer : public CCLayerColor {
  2. public:
  3. static CCScene* scene();
  4. CREATE_FUNC(MenuLayer);
  5. private:
  6. bool init();
  7. void addTarena();
  8. void addBackGround();
  9. void addLogo();
  10. void addMenu();
  11. void onStarGame(CCObject* sender);
  12. };

首先实现场景创建方法scene,该方法中创建场景并添加菜单层,代码如下所示:

    
    
    
    
  1. //创建场景
  2. CCScene* MenuLayer::scene() {
  3. CCScene *scene = CCScene::create();
  4. MenuLayer *menuLayer = MenuLayer::create();
  5. scene->addChild(menuLayer);
  6. return scene;
  7. }

接下来实现init方法,该方法中通过调用addTarena方法添加欢迎界面,欢迎界面是一个logo的显示和消失,代码如下所示:

    
    
    
    
  1. bool MenuLayer::init() {
  2. if (!CCLayerColor::initWithColor(ccc4(255, 255, 255, 255))) return false;
  3. //添加菜单的欢迎界面
  4. this->addTarena();
  5. return true;
  6. }

实现addTarena方法,先创建一个tarena图片的精灵到界面中,然后创建一个CCFadeIn类型的从无到有的渐变效果,接下来是一个fadeOut类型的从有到无的渐变效果,最后添加回调函数addBackGround,将以上动作放入一个顺时执行动作中,代码如下所示:

    
    
    
    
  1. void MenuLayer::addTarena() {
  2. //添加tarena的logo,并添加显示和消失动作
  3. CCSprite *tarena = CCSprite::create("Menu/tarena.jpg");
  4. this->addChild(tarena);
  5. tarena->setPosition(SCREEN_CENTER);
  6. tarena->setOpacity(0);
  7. CCDelayTime *delay = CCDelayTime::create(1);
  8. //从无到有的渐变效果
  9. CCFadeIn *fadeIn = CCFadeIn::create(1);
  10. CCDelayTime *delay2 = CCDelayTime::create(1);
  11. //从有到无的渐变效果
  12. CCFadeOut *fadeOut = CCFadeOut::create(1);
  13. //回调函数
  14. CCCallFunc *callFunc = CCCallFunc::create(this, callfunc_selector(MenuLayer::addBackGround));
  15. //将以上所有动作放入一个顺时动作中
  16. CCSequence *seq = CCSequence::create(delay, fadeIn, delay2,callFunc, fadeOut, NULL);
  17. tarena->runAction(seq);
  18. }

接下来实现回调函数addBackGround添加菜单层的背景,以及背景的入场动作,代码如下所示:

    
    
    
    
  1. //添加菜单层的背景
  2. void MenuLayer::addBackGround() {
  3. CCSprite *bg = CCSprite::create("Menu/img_bg_logo.jpg");
  4. bg->setAnchorPoint(CCPointZero);
  5. bg->setOpacity(0);
  6. this->addChild(bg);
  7. //背景图片的入场动作
  8. CCFadeIn *fadeIn = CCFadeIn::create(1);
  9. CCMoveTo *moveTo = CCMoveTo::create(1, ccp(0, SCREEN.height - bg->getContentSize().height));
  10. CCCallFunc *callFunc = CCCallFunc::create(this, callfunc_selector(MenuLayer::addLogo));
  11. CCSequence *seq = CCSequence::create(fadeIn, moveTo, callFunc, NULL);
  12. bg->runAction(seq);
  13. }

实现回调函数logo,添加logo精灵和入场动作,代码如下所示:

    
    
    
    
  1. //添加游戏logo
  2. void MenuLayer::addLogo() {
  3. CCSprite *logo = CCSprite::create("Menu/LOGO.png");
  4. logo->setPosition(ccp(-200, 200));
  5. this->addChild(logo);
  6. CCMoveTo *moveTo = CCMoveTo::create(1, SCREEN_POSITION(0.5, 0.8));
  7. CCEaseBounceOut *bounce = CCEaseBounceOut::create(moveTo);
  8. CCCallFunc *callFunc = CCCallFunc::create(this, callfunc_selector(MenuLayer::addMenu));
  9. CCSequence *seq = CCSequence::create(callFunc, bounce, NULL);
  10. logo->runAction(seq);
  11. }

最后实现回调函数addMenu,添加菜单项和菜单,代码如下所示:

    
    
    
    
  1. //添加菜单和菜单项
  2. void MenuLayer::addMenu() {
  3. CCMenu *menu = CCMenu::create();
  4. CCLabelBMFont *bmFont = CCLabelBMFont::create("Start", "font/bitmapFontTest.fnt");
  5. CCMenuItemLabel *itemLabel = CCMenuItemLabel::create(bmFont, this, menu_selector(MenuLayer::onStarGame));
  6. itemLabel->setScale(0);
  7. CCRotateBy *rotate = CCRotateBy::create(1, 1800);
  8. CCScaleTo *scaleTo = CCScaleTo::create(1, 1);
  9. CCSpawn *spawn = CCSpawn::create(rotate, scaleTo, NULL);
  10. itemLabel->runAction(spawn);
  11. menu->addChild(itemLabel);
  12. this->addChild(menu);
  13. }
  14. //游戏开始事件,切换到游戏场景
  15. void MenuLayer::onStarGame(CCObject* sender) {
  16. CCDirector::sharedDirector()->replaceScene(CCTransitionFade::create(1, MapLayer::scene(1)));
  17. CCLog("游戏开始");
  18. }

步骤二:创建地图层MapLayer

创建一个地图层MapLayer,继承至CCLayer,该层用于创建游戏的背景地图,游戏滚动地图的实现由两个地图背景精灵交替滚动实现,代码如下所示:

    
    
    
    
  1. class MapLayer : public CCLayer {
  2. public:
  3. static CCScene* scene(int Level);
  4. static MapLayer* createMapLayer(int Level);
  5. private:
  6. //两个地图精灵
  7. CCSprite *m_map1;
  8. CCSprite *m_map2;
  9. int m_level;
  10. bool initMapLayer(int Level);
  11. void initMap();
  12. void mapMove(float time);
  13. };

首先实现场景创建方法scene,该方法中创建场景并添加地图层,代码如下所示:

    
    
    
    
  1. //创建场景
  2. CCScene* MapLayer::scene(int Level) {
  3. CCScene *scene = CCScene::create();
  4. //地图层
  5. MapLayer *mapLayer = MapLayer::createMapLayer(Level);
  6. scene->addChild(mapLayer);
  7. return scene;
  8. }

然后实现createMapLayer方法和initMapLayer方法创建地图层并初始化地图,代码如下所示:

    
    
    
    
  1. //创建地图层
  2. MapLayer* MapLayer::createMapLayer(int Level) {
  3. MapLayer *mapLayer = new MapLayer();
  4. if (mapLayer && mapLayer->initMapLayer(Level)) {
  5. mapLayer->autorelease();
  6. return mapLayer;
  7. }
  8. CC_SAFE_DELETE(mapLayer);
  9. return NULL;
  10. }
  11. bool MapLayer::initMapLayer(int Level) {
  12. if (!CCLayer::init()) return false;
  13. m_level = Level;
  14. //初始化地图
  15. this->initMap();
  16. return true;
  17. }

实现方法initMap,根据当前等级初始化不同的地图文件,为了实现滚动地图需要创建两个地图精灵,代码如下所示:

    
    
    
    
  1. void MapLayer::initMap() {
  2. //根据当前等级初始化不同的地图文件
  3. const char *imageName = CCString::createWithFormat("BG/img_bg_level_%d.jpg", m_level)->getCString();
  4. m_map1 = CCSprite::create(imageName);
  5. m_map1->setAnchorPoint(CCPointZero);
  6. this->addChild(m_map1);
  7. m_map2 = CCSprite::create(imageName);
  8. m_map2->setAnchorPoint(CCPointZero);
  9. m_map2->setPosition(ccp(0, m_map1->getContentSize().height));
  10. this->addChild(m_map2);
  11. this->schedule(schedule_selector(MapLayer::mapMove));
  12. }

最后实现地图的滚动,通过地图移动和坐标由两个地图交替显示,代码如下所示:

    
    
    
    
  1. void MapLayer::mapMove(float time) {
  2. //地图滚动及滚轴地图逻辑
  3. m_map1->setPositionY(m_map1->getPositionY() - map_speed);
  4. m_map2->setPositionY(m_map2->getPositionY() - map_speed);
  5. if (m_map1->getPositionY() <= -m_map1->getContentSize().height) {
  6. m_map1->setPositionY(m_map2->getContentSize().height + m_map2->getPositionY());
  7. }
  8. if (m_map2->getPositionY() <= -m_map2->getContentSize().height) {
  9. m_map2->setPositionY(m_map1->getContentSize().height + m_map1->getPositionY());
  10. }
  11. }

1.4 完整代码

本案例中,AppDelegate.cpp文件中的完整代码如下所示:

    
    
    
    
  1. #include "AppDelegate.h"
  2. #include "MenuLayer.h"
  3. #include "MapLayer.h"
  4. USING_NS_CC;
  5. AppDelegate::AppDelegate() {
  6. }
  7. AppDelegate::~AppDelegate()
  8. {
  9. }
  10. bool AppDelegate::applicationDidFinishLaunching() {
  11. // initialize director
  12. CCDirector* pDirector = CCDirector::sharedDirector();
  13. CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();
  14. pDirector->setOpenGLView(pEGLView);
  15. //屏幕适配
  16. pEGLView->setDesignResolutionSize(320, 568, kResolutionExactFit);
  17. pDirector->setDisplayStats(true);
  18. pDirector->setAnimationInterval(1.0 / 60);
  19. CCScene *pScene = MenuLayer::scene();
  20. pDirector->runWithScene(pScene);
  21. return true;
  22. }
隐藏

本案例中,MenuLayer.h文件中的完整代码如下所示:

    
    
    
    
  1. #ifndef __Plane__MenuLayer__
  2. #define __Plane__MenuLayer__
  3. #include <stdio.h>
  4. #include "cocos2d.h"
  5. USING_NS_CC;
  6. //继承带颜色的层
  7. class MenuLayer : public CCLayerColor {
  8. public:
  9. static CCScene* scene();
  10. CREATE_FUNC(MenuLayer);
  11. private:
  12. bool init();
  13. void addTarena();
  14. void addBackGround();
  15. void addLogo();
  16. void addMenu();
  17. void onStarGame(CCObject* sender);
  18. };
  19. #endif /* defined(__Plane__MenuLayer__) */
隐藏

本案例中,MenuLayer.cpp文件中的完整代码如下所示:

    
    
    
    
  1. #include "MenuLayer.h"
  2. #include "MapLayer.h"
  3. #include "Tools.h"
  4. //创建场景
  5. CCScene* MenuLayer::scene() {
  6. CCScene *scene = CCScene::create();
  7. MenuLayer *menuLayer = MenuLayer::create();
  8. scene->addChild(menuLayer);
  9. return scene;
  10. }
  11. //初始化方法
  12. bool MenuLayer::init() {
  13. if (!CCLayerColor::initWithColor(ccc4(255, 255, 255, 255))) return false;
  14. //添加菜单的欢迎界面
  15. this->addTarena();
  16. return true;
  17. }
  18. //菜单的欢迎界面
  19. void MenuLayer::addTarena() {
  20. //添加tarena的logo,并添加显示和消失动作
  21. CCSprite *tarena = CCSprite::create("Menu/tarena.jpg");
  22. this->addChild(tarena);
  23. tarena->setPosition(SCREEN_CENTER);
  24. tarena->setOpacity(0);
  25. CCDelayTime *delay = CCDelayTime::create(1);
  26. //从无到有的渐变效果
  27. CCFadeIn *fadeIn = CCFadeIn::create(1);
  28. CCDelayTime *delay2 = CCDelayTime::create(1);
  29. //从有到无的渐变效果
  30. CCFadeOut *fadeOut = CCFadeOut::create(1);
  31. //回调函数
  32. CCCallFunc *callFunc = CCCallFunc::create(this, callfunc_selector(MenuLayer::addBackGround));
  33. //将以上所有动作放入一个顺时动作中
  34. CCSequence *seq = CCSequence::create(delay, fadeIn, delay2,callFunc, fadeOut, NULL);
  35. tarena->runAction(seq);
  36. }
  37. //添加菜单层的背景
  38. void MenuLayer::addBackGround() {
  39. CCSprite *bg = CCSprite::create("Menu/img_bg_logo.jpg");
  40. bg->setAnchorPoint(CCPointZero);
  41. bg->setOpacity(0);
  42. this->addChild(bg);
  43. CCFadeIn *fadeIn = CCFadeIn::create(1);
  44. CCMoveTo *moveTo = CCMoveTo::create(1, ccp(0, SCREEN.height - bg->getContentSize().height));
  45. CCCallFunc *callFunc = CCCallFunc::create(this, callfunc_selector(MenuLayer::addLogo));
  46. CCSequence *seq = CCSequence::create(fadeIn, moveTo, callFunc, NULL);
  47. bg->runAction(seq);
  48. }
  49. //添加游戏logo
  50. void MenuLayer::addLogo() {
  51. CCSprite *logo = CCSprite::create("Menu/LOGO.png");
  52. logo->setPosition(ccp(-200, 200));
  53. this->addChild(logo);
  54. CCMoveTo *moveTo = CCMoveTo::create(1, SCREEN_POSITION(0.5, 0.8));
  55. CCEaseBounceOut *bounce = CCEaseBounceOut::create(moveTo);
  56. CCCallFunc *callFunc = CCCallFunc::create(this, callfunc_selector(MenuLayer::addMenu));
  57. CCSequence *seq = CCSequence::create(callFunc, bounce, NULL);
  58. logo->runAction(seq);
  59. }
  60. //添加菜单和菜单项
  61. void MenuLayer::addMenu() {
  62. CCMenu *menu = CCMenu::create();
  63. CCLabelBMFont *bmFont = CCLabelBMFont::create("Start", "font/bitmapFontTest.fnt");
  64. CCMenuItemLabel *itemLabel = CCMenuItemLabel::create(bmFont, this, menu_selector(MenuLayer::onStarGame));
  65. itemLabel->setScale(0);
  66. CCRotateBy *rotate = CCRotateBy::create(1, 1800);
  67. CCScaleTo *scaleTo = CCScaleTo::create(1, 1);
  68. CCSpawn *spawn = CCSpawn::create(rotate, scaleTo, NULL);
  69. itemLabel->runAction(spawn);
  70. menu->addChild(itemLabel);
  71. this->addChild(menu);
  72. }
  73. //游戏开始事件,切换到游戏场景
  74. void MenuLayer::onStarGame(CCObject* sender) {
  75. CCDirector::sharedDirector()->replaceScene(CCTransitionFade::create(1, MapLayer::scene(1)));
  76. CCLog("游戏开始");
  77. }
隐藏

本案例中,MapLayer.h文件中的完整代码如下所示:

    
    
    
    
  1. #ifndef __Plane__MapLayer__
  2. #define __Plane__MapLayer__
  3. #include <stdio.h>
  4. #include "cocos2d.h"
  5. USING_NS_CC;
  6. #define map_speed 1
  7. class MapLayer : public CCLayer {
  8. public:
  9. static CCScene* scene(int Level);
  10. static MapLayer* createMapLayer(int Level);
  11. private:
  12. //两个地图精灵
  13. CCSprite *m_map1;
  14. CCSprite *m_map2;
  15. int m_level;
  16. bool initMapLayer(int Level);
  17. void initMap();
  18. void mapMove(float time);
  19. };
  20. #endif /* defined(__Plane__MapLayer__) */
隐藏

本案例中,MapLayer.cpp文件中的完整代码如下所示:

    
    
    
    
  1. #include "MapLayer.h"
  2. //创建场景
  3. CCScene* MapLayer::scene(int Level) {
  4. CCScene *scene = CCScene::create();
  5. //地图层
  6. MapLayer *mapLayer = MapLayer::createMapLayer(Level);
  7. scene->addChild(mapLayer);
  8. return scene;
  9. }
  10. //创建地图层
  11. MapLayer* MapLayer::createMapLayer(int Level) {
  12. MapLayer *mapLayer = new MapLayer();
  13. if (mapLayer && mapLayer->initMapLayer(Level)) {
  14. mapLayer->autorelease();
  15. return mapLayer;
  16. }
  17. CC_SAFE_DELETE(mapLayer);
  18. return NULL;
  19. }
  20. bool MapLayer::initMapLayer(int Level) {
  21. if (!CCLayer::init()) return false;
  22. m_level = Level;
  23. //初始化地图
  24. this->initMap();
  25. return true;
  26. }
  27. void MapLayer::initMap() {
  28. //根据当前等级 初始化不同的 地图文件
  29. const char *imageName = CCString::createWithFormat("BG/img_bg_level_%d.jpg", m_level)->getCString();
  30. m_map1 = CCSprite::create(imageName);
  31. m_map1->setAnchorPoint(CCPointZero);
  32. this->addChild(m_map1);
  33. m_map2 = CCSprite::create(imageName);
  34. m_map2->setAnchorPoint(CCPointZero);
  35. m_map2->setPosition(ccp(0, m_map1->getContentSize().height));
  36. this->addChild(m_map2);
  37. this->schedule(schedule_selector(MapLayer::mapMove));
  38. }
  39. void MapLayer::mapMove(float time) {
  40. //地图滚动及滚轴地图逻辑
  41. m_map1->setPositionY(m_map1->getPositionY() - map_speed);
  42. m_map2->setPositionY(m_map2->getPositionY() - map_speed);
  43. if (m_map1->getPositionY() <= -m_map1->getContentSize().height) {
  44. m_map1->setPositionY(m_map2->getContentSize().height + m_map2->getPositionY());
  45. }
  46. if (m_map2->getPositionY() <= -m_map2->getContentSize().height) {
  47. m_map2->setPositionY(m_map1->getContentSize().height + m_map1->getPositionY());
  48. }
  49. }
隐藏

本案例中,Tools.h文件中的完整代码如下所示:

    
    
    
    
  1. #ifndef Plane_Tools_h
  2. #define Plane_Tools_h
  3. #define SCREEN CCDirector::sharedDirector()->getWinSize()
  4. #define SCREEN_CENTER ccp(SCREEN.width * 0.5, SCREEN.height * 0.5)
  5. #define SCREEN_POSITION(_x, _y) ccp(SCREEN.width * _x, SCREEN.height * _y)
  6. #define SCREEN_POSITION_X(_x) (SCREEN.width + _x)
  7. #define SCREEN_POSITION_Y(_y) (SCREEN.height + _y)
  8. #endif



你可能感兴趣的:(cocos2d-x)