coco2d-x 2.0学习系列(1):SimpleGame(1)

coco2d-x 2.0学习系列(一):SimpleGame(1)

coco2d-x 2.0 与1.0版本有些细节的区别,网上的SimpleGame大部分是1.0的,导致采了很多的坑。

1.使用VS2010C创建Cocos2d-win32 Application,名为SimpleGame去掉Box2D选项。

2.新建头文件SimpleGameScene.h和源文件SimpleGameScene.cpp

SimpleGameScene.h

 

#ifndef __SIMPLE_GAME_H__
#define __SIMPLE_GAME_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(float dt);// 这个地方是个坑1.0版本使用的ccTime 2.0使用float替代
	CREATE_FUNC(SimpleGame);//这个地方也是坑1.0版本LAYER_NODE_FUNC
};

#endif

 SimpleGameScene.cpp

 

#include "SimpleGameScene.h"
using namespace cocos2d;
SimpleGame::SimpleGame(){}
SimpleGame::~SimpleGame(){}
CCScene* SimpleGame::scene(){
	CCScene* scene=NULL;
	do 
	{
		 scene=CCScene::create();
		CC_BREAK_IF(!scene);
		SimpleGame *layer=SimpleGame::create();
		CC_BREAK_IF(!layer);
		scene->addChild(layer);
	} while (0);
	return scene;
}
//初始化
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::itemWithNormalImage(
			"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);
		this->addChild(pMenu,1);//添加到simplegame layer
		//创建精灵
		CCSprite* hero=CCSprite::spriteWithFile("Target.png",CCRectMake(0,0,27,40));
		CC_BREAK_IF(! hero);
		hero->setPosition(ccp(hero->getContentSize().width/2,size.height/2));
		this->addChild(hero,0);
			this->schedule( schedule_selector(SimpleGame::gameLogic), 1.0 );//每隔一秒调用gameLogic()添加精灵
			bRet=true;
	} while (0);
	return bRet;
}
void SimpleGame::addEnemy(){
	CCSprite *enemy=CCSprite::spriteWithFile("Player.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( (float)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(float dt)
{
	this->addEnemy();
}

void SimpleGame::menuCloseCallback(CCObject* pSender)
{
	// "close" menu item clicked
	CCDirector::sharedDirector()->end();
}

 最后运行之前别忘了修改AppDelegate.cpp,把HelloWorldScene改成自己定义的SimpleGame。

 

CCScene *pScene = SimpleGame::scene();