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

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);
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/118344.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号