使用cocos2d-x 2.0.1 自动循环切换场景

由于cocos2d-x 2.0.1 的引擎里面改了很多方法明, 比如新建的场景由node改为create等。


下面是我使用最新的引擎写的一个简单的切换场景。 


 要切换的头文件 里面有继承  CCLayerColor跟CCScene场景类。

下面是头文件GameOver.h

#pragma once

#include "cocos2d.h"
using namespace cocos2d;
class GameOverLayer2 : public cocos2d::CCLayerColor
{
public:
	GameOverLayer2(void);
	~GameOverLayer2(void);

	bool init();

	void gameOverDone();

	LAYER_CREATE_FUNC(GameOverLayer2);
	CC_SYNTHESIZE_READONLY(CCLabelTTF *,_ttf,Ttf);

};

class GameOverScene2 : public cocos2d::CCScene
{
public:

	~GameOverScene2();

	bool init();
	
	SCENE_CREATE_FUNC(GameOverScene2);
	CC_SYNTHESIZE_READONLY(GameOverLayer2 *,_layer,Layer);

};
下面是GameOver.cpp文件

#include "GameOver.h"
#include "HelloWorldScene.h"

GameOverLayer2::GameOverLayer2(void)
{
}

GameOverLayer2::~GameOverLayer2(void)
{
	if(_ttf)
	{
		_ttf->release();
		_ttf = NULL;		
	}	
}


void GameOverLayer2::gameOverDone()
{
	CCDirector::sharedDirector()->replaceScene(HelloWorld::scene());
}

bool GameOverLayer2::init()
{
	if (CCLayerColor::initWithColor(cocos2d::ccc4(255,255,255,255)))
	{
		CCSize size = CCDirector::sharedDirector()->getWinSize();

		this->_ttf = CCLabelTTF::create("","Artial",33);
		//没有使用retain保留会报堆栈出错
		this->_ttf->retain();
		this->_ttf->setPosition(ccp(size.width/2,size.height/2));

		this->addChild(_ttf);

		this->runAction(CCSequence::create(
			CCDelayTime::create(3),CCCallFunc::create(this,callfunc_selector(GameOverLayer2::gameOverDone)),NULL));
		return true;
	}

	return false;
}

GameOverScene2::~GameOverScene2()
{

	if(_layer)
	{
		_layer->release();
		_layer = NULL;
	}
}

bool GameOverScene2::init()
{
	if(CCScene::init())
	{
		_layer = GameOverLayer2::create();	
		_layer->retain();
		this->addChild(_layer);
		return true;
	}
	return false;
}

然后可以在HelloWorldScene.cpp的 init 函数体里加入

this->runAction(CCSequence::create(
			CCDelayTime::create(3),
			CCCallFunc::create(this,
					callfunc_selector(HelloWorld::changeScene)),NULL));

同时在HelloWorldScene.cpp 写个方法changeScene

代码如下:

void HelloWorld::changeScene()
{
	//GameOverScene* gameScene = GameOverScene::create();
	GameOverScene2* gameScene = GameOverScene2::create();
	gameScene->getLayer()->getTtf()->setString("where my love~~");

	CCDirector::sharedDirector()->replaceScene(gameScene);

}
记住要引用头文件。~  


这样你会发现场景每3秒切换一次。


 ~~  ok!


你可能感兴趣的:(null,Class,引擎,layer,ttf)