Cocos2D-X学习笔记 3 从一个场景切换到还有一个场景

工厂方法一般写法

StartLayer * StartLayer::create()
{
StartLayer *sl = new StartLayer();
sl->init();
sl->autorelease();
return sl;
}

也能够用CC的CCPlatformMacros.h文件中提供的宏 CREATE_FUNC 来替代上面一段代码

#define CREATE_FUNC(__TYPE__) \
static __TYPE__* create() \
{ \
    __TYPE__ *pRet = new __TYPE__(); \
    if (pRet && pRet->init()) \
    { \
        pRet->autorelease(); \
        return pRet; \
    } \
    else \
    { \
        delete pRet; \
        pRet = NULL; \
        return NULL; \
    } \
}

----------------------------------------------------从StartLayer LoadingLayer---------------------------------------------------------

StartLayer.h

#pragma once
#include "cocos2d.h"
using namespace cocos2d;
class StartLayer : public CCLayer
{
public:
StartLayer();
virtual ~StartLayer();
bool init();
static StartLayer * create();
static CCScene * scene();
private:
void clickSigle(CCObject *sender);
};

StartLayer.cpp

#include "StartLayer.h"
#include "LoadingLayer.h"

StartLayer::StartLayer(void)
{
}

StartLayer::~StartLayer(void)
{
}


bool StartLayer::init()
{
if(!CCLayer::init())
{
return false;
}

//開始界面背景图片
CCSize screenSize = CCDirector::sharedDirector()->getWinSize();
CCSprite *bgSprite = CCSprite::create("BackGround.png");
bgSprite->setPosition(CCPoint(screenSize.width/2,screenSize.height/2));
this->addChild(bgSprite);

//button的使用
CCMenuItemImage *singleItem = CCMenuItemImage::create("btn1.png","btn2.png",this,menu_selector(StartLayer::clickSigle));
singleItem->setPosition(CCPointMake(80,-80));
CCMenu *menu = CCMenu::create(singleItem,NULL);
addChild(menu);

return true;
}

void StartLayer::clickSigle(CCObject *sender)
{
CCLOG("click sigle");

//跳转到loadingLayer界面

//用2s 动画来切换界面
CCScene *s = LoadingLayer::scene();

CCTransitionSplitRows *trans = CCTransitionSplitRows::create(2,s);
CCDirector::sharedDirector()->replaceScene(trans);
}

StartLayer * StartLayer::create()
{
StartLayer *sl = new StartLayer();
sl->init();
sl->autorelease();
return sl;
}

CCScene * StartLayer::scene()
{
StartLayer *sl = StartLayer::create();
CCScene *s = CCScene::create();
s->addChild(sl);
return s;
}

------------------------------------------------------------------------------------------

LoadingLayer.h

#pragma once
#include "cocos2d.h"
USING_NS_CC;


class LoadingLayer : public CCLayer
{
public:
int count;
LoadingLayer(void);
~LoadingLayer(void);
bool init();
static LoadingLayer * create();
static CCScene * scene();

//生命周期
    virtual void onEnter();
    virtual void onExit();
    virtual void onEnterTransitionDidFinish();


private:
void timeUpdate(float dt);


};


LoadingLayer.cpp

#include "LoadingLayer.h"
LoadingLayer::LoadingLayer(void)
{
}

LoadingLayer::~LoadingLayer(void)
{
}

bool LoadingLayer::init()
{
if(!CCLayer::init())
{
return false;
}

//载入载入界面背景图片
CCSprite *bgSprite = CCSprite::create("loading.png");
CCSize screenSize = CCDirector::sharedDirector()->getWinSize();
bgSprite->setPosition(ccp(screenSize.width/2,screenSize.height/2));
this->addChild(bgSprite);

//label的使用
CCLabelTTF* label = CCLabelTTF::create("loading","Arial",25);
label->setPosition(ccp(100,150));
label->setAnchorPoint(ccp(0,0.5));
label->setTag(100);
addChild(label);

count = 1;
return true;
}


void LoadingLayer::timeUpdate(float dt)
{

//拼接字符串
CCLabelTTF *label = (CCLabelTTF*)getChildByTag(100);
const char * str = label->getString();
char newstr[128];
if(++count > 5)
{
//停止定时器
unschedule(schedule_selector(LoadingLayer::timeUpdate));
}
sprintf(newstr,"%s.",str);
label->setString(newstr);


}

LoadingLayer* LoadingLayer::create()
{
LoadingLayer* layer = new LoadingLayer();
layer->init();
layer->autorelease();
return layer;
}


CCScene* LoadingLayer::scene()
{
LoadingLayer* layer = LoadingLayer::create();
CCScene * s = CCScene::create();
s->addChild(layer);
return s;
}


void LoadingLayer::onEnter()
{
CCLayer::onEnter();
}


void LoadingLayer::onExit()
{
CCLayer::onExit();
}


void LoadingLayer::onEnterTransitionDidFinish()
{
CCLayer::onEnterTransitionDidFinish();

//系统有一个轻量的定时器,每隔0.5s会调用this,timeUpdate方法
this->schedule(schedule_selector(LoadingLayer::timeUpdate),0.5f);
}





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