#ifndef __GAME_SCENE_H__ #define __GAME_SCENE_H__ #include "cocos2d.h" class SimpleGame : public cocos2d::CCLayerColor { public: SimpleGame(); ~SimpleGame(); virtual bool init(); static cocos2d::CCScene* scene(); //退出游戏菜单回调函数 virtual void menuCloseCallback(CCObject* pSender); void addEnemy(); void spriteMoveFinished(cocos2d::CCNode *sender); void gameLogic(cocos2d::ccTime dt); LAYER_NODE_FUNC(SimpleGame); }; #endifSimpleGame继承了CCLayerColor,这样就可以指定游戏的背景颜色。LAYER_NODE_FUNC这个宏在引擎内部(CCLayer.h)被定义。主要是为自己建的层增加一个node()函数,node()函数的作用是new一个实例,并执行实例的init方法(所以必须得定义init方法),将它加入autorelease,也就是所有由node()函数得来的指针,都不需要手动的释放(除非自己调用了retain())。源码如下
#define LAYER_NODE_FUNC(layer) \ static layer* node() \ { \ layer *pRet = new layer(); \ if (pRet && pRet->init()) \ { \ pRet->autorelease(); \ return pRet; \ } \ else \ { \ delete pRet; \ pRet = NULL; \ return NULL; \ } \ };
#include "GameScene.h" using namespace cocos2d; SimpleGame::SimpleGame() { } SimpleGame::~SimpleGame() { } CCScene* SimpleGame::scene() { CCScene * scene = NULL; do { // 'scene' is an autorelease object scene = CCScene::node(); CC_BREAK_IF(! scene); // 'layer' is an autorelease object SimpleGame *layer = SimpleGame::node(); CC_BREAK_IF(! layer); // add layer as a child to scene scene->addChild(layer); } while (0); // return the scene return scene; } // on "init" you need to initialize your instance bool SimpleGame::init() { bool bRet = false; do { CC_BREAK_IF(! CCLayerColor::initWithColor(ccc4(255,255,255,255))); CCSize size = CCDirector::sharedDirector()->getWinSize(); //添加一个退出游戏按钮菜单 CCMenuItemImage *pCloseItem = CCMenuItemImage::itemFromNormalImage( "CloseNormal.png", "CloseSelected.png", this, menu_selector(SimpleGame::menuCloseCallback)); CC_BREAK_IF(! pCloseItem); pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20)); CCMenu* pMenu = CCMenu::menuWithItems(pCloseItem, NULL); pMenu->setPosition(CCPointZero); CC_BREAK_IF(! pMenu); // Add the menu to SimpleGame layer as a child layer. this->addChild(pMenu, 1); // 3. Add add a splash screen, show the cocos2d splash image. CCSprite* hero = CCSprite::spriteWithFile("Hero.png",CCRectMake(0,0,27,40)); CC_BREAK_IF(! hero); //默认值 //player->setAnchorPoint(ccp(0.5,0.5)); // Place the sprite on the center of the screen hero->setPosition(ccp(hero->getContentSize().width/2, size.height/2)); // Add the sprite to SimpleGame layer as a child layer. this->addChild(hero, 0); this->schedule(schedule_selector(SimpleGame::gameLogic),1.0); bRet = true; } while (0); return bRet; } void SimpleGame::addEnemy() { CCSprite *enemy = CCSprite::spriteWithFile("Enemy.png",CCRectMake(0,0,27,40)); CCSize winSize = CCDirector::sharedDirector()->getWinSize(); int minY = enemy->getContentSize().height / 2; int maxY = winSize.height - enemy->getContentSize().height / 2; int rangeY = maxY - minY; int actualY = (rand() % rangeY) + minY; enemy->setPosition(ccp(winSize.width + enemy->getContentSize().width / 2,actualY)); this->addChild(enemy); int minDuration = 2; int maxDuration = 4; int rangeDuration = maxDuration - minDuration; int actualDuration = (rand() % rangeDuration) + minDuration; CCFiniteTimeAction *actionMove = CCMoveTo::actionWithDuration((ccTime)actualDuration,ccp(0 - enemy->getContentSize().width / 2,actualY)); CCFiniteTimeAction *actionMoveDone = CCCallFuncN::actionWithTarget(this,callfuncN_selector(SimpleGame::spriteMoveFinished)); enemy->runAction(CCSequence::actions(actionMove,actionMoveDone,NULL)); } void SimpleGame::spriteMoveFinished(CCNode *sender) { CCSprite *sprite = (CCSprite *)sender; this->removeChild(sprite,true); } void SimpleGame::gameLogic(ccTime dt) { this->addEnemy(); } void SimpleGame::menuCloseCallback(CCObject* pSender) { // "close" menu item clicked CCDirector::sharedDirector()->end(); }
typedef void (CCObject::*SEL_SCHEDULE)(ccTime); #define schedule_selector(_SELECTOR) (SEL_SCHEDULE)(&_SELECTOR)
回调函数gameLogic的格式必须为gameLogic(ccTime dt)。
最后运行之前别忘了修改AppDelegate.cpp,把HelloWorldScene改成自己定义的SimpleGame。
CCScene *pScene = SimpleGame::scene();
ctrl+f5,运行程序,效果如图,可以看到一个黑衣忍者站在屏幕最左端,一群黑怪从屏幕右端飞奔而来:
转载请注明来自:Alex Zhou,本文链接:http://codingnow.cn/cocos2d-x/680.html