SceneA.h
#ifndef __ALayer_SCENE_H__
#define __ALayer_SCENE_H__
#include "cocos2d.h"
#include "SceneB.h"
class ALayer : public cocos2d::Layer ,public SceneBDelegator
{
public:
// there's no 'id' in cpp, so we recommend returning the class instance Vec2er
static cocos2d::Scene* createScene();
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();
// 进入下一个场景.
void menEnterNextScene(cocos2d::Ref* pSender);
virtual void callBack(void *ctx, const char *str);
// implement the "static create()" method manually
CREATE_FUNC(ALayer);
};
#endif // __ALayer_SCENE_H__
SceneA.cpp
#include "SceneA.h"
USING_NS_CC;
Scene* ALayer::createScene()
{
// 'scene' is an autorelease object
auto scene = Scene::create();
// 'layer' is an autorelease object
auto layer = ALayer::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool ALayer::init()
{
//
// 1. super init first
if ( !Layer::init() )
{
return false;
}
Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
auto staticLabel = Label::createWithBMFont("fonts/fnt9.fnt", "Scene A");
staticLabel->setPosition(Vec2(visibleSize.width/2,
visibleSize.height - 50));
this->addChild(staticLabel, 1);
auto label = Label::createWithTTF("", "fonts/Marker Felt.ttf", 24);
label->setPosition(Vec2(visibleSize.width/2,
visibleSize.height - 200));
this->addChild(label, 1, 100);
auto pItmLabel = Label::createWithBMFont("fonts/fnt9.fnt", "Next");
auto pItmMenu = MenuItemLabel::create(pItmLabel, CC_CALLBACK_1(ALayer::menEnterNextScene, this));
Menu* mn = Menu::create(pItmMenu, NULL);
mn->alignItemsVertically();
this->addChild(mn);
return true;
}
// 进入下一个场景
void ALayer::menEnterNextScene(Ref* pSender)
{
auto sc = Scene::create();
auto layer = BLayer::create();
layer->setDelegator(this);
sc->addChild(layer);
auto reScene = TransitionSlideInR::create(1.0f, sc);
Director::getInstance()->pushScene(reScene);
}
void ALayer::callBack(void *ctx, const char *str)
{
log("ALayer callBack");
Label* label = (Label*)this->getChildByTag(100);
if (label)
label->setString(str);
}
SceneB.h
#ifndef __BLayer_H__
#define __BLayer_H__
#include "cocos2d.h"
class SceneBDelegator
{
public:
virtual ~SceneBDelegator() {}
//回调委托对象
virtual void callBack(void *ctx, const char *str) = 0;
};
class BLayer : public cocos2d::Layer
{
SceneBDelegator* _delegator;
public:
static cocos2d::Scene* createScene();
virtual bool init();
// 更新单例对象状态.
void menUpdate(cocos2d::Ref* pSender);
// 返回上一个场景.
void menReturnPreviousScene(cocos2d::Ref* pSender);
void setDelegator(SceneBDelegator* delegator);
// implement the "static create()" method manually
CREATE_FUNC(BLayer);
};
#endif // __BLayer_H__
SceneB.cpp
#include "SceneB.h"
USING_NS_CC;
Scene* BLayer::createScene()
{
// 'scene' is an autorelease object
auto scene = Scene::create();
// 'layer' is an autorelease object
auto layer = BLayer::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool BLayer::init()
{
//
// 1. super init first
if ( !Layer::init() )
{
return false;
}
Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
auto staticLabel = Label::createWithBMFont("fonts/fnt9.fnt", "Scene B");
staticLabel->setPosition(Vec2(visibleSize.width/2,
visibleSize.height - 50));
this->addChild(staticLabel, 1);
auto label = Label::createWithTTF("", "fonts/Marker Felt.ttf", 24);
label->setPosition(Vec2(visibleSize.width/2,
visibleSize.height - 200));
this->addChild(label, 1, 100);
auto pItmLabel1 = Label::createWithBMFont("fonts/fnt8.fnt", "Update Status");
auto pItmMenu1 = MenuItemLabel::create(pItmLabel1, CC_CALLBACK_1(BLayer::menUpdate, this));
auto pItmLabel2 = Label::createWithBMFont("fonts/fnt8.fnt", "Return");
auto pItmMenu2 = MenuItemLabel::create(pItmLabel2, CC_CALLBACK_1(BLayer::menReturnPreviousScene, this));
Menu* mn = Menu::create(pItmMenu1,pItmMenu2, NULL);
mn->alignItemsVertically();
this->addChild(mn);
return true;
}
void BLayer::menUpdate(Ref* pSender)
{
int num = CCRANDOM_0_1() * 1000;
__String* s = __String::createWithFormat("SceneA Update %d", num);
//回调HelloWorld场景
_delegator->callBack(this, s->getCString());
log("%s", s->getCString());
}
void BLayer::menReturnPreviousScene(Ref* pSender)
{
Director::getInstance()->popScene();
}
void BLayer::setDelegator(SceneBDelegator* delegator)
{
_delegator = delegator;
}