SceneA.h
#ifndef __ALayer_SCENE_H__
#define __ALayer_SCENE_H__
#include "cocos2d.h"
#include "SceneB.h"
#define MSG_STATE "scene_notification"
class ALayer : public cocos2d::Layer
{
public:
~ALayer();
// there's no 'id' in cpp, so we recommend returning the class instance pointer
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);
void callBack(cocos2d::Ref *sender);
// 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/fnt8.fnt", "Next");
auto pItmMenu = MenuItemLabel::create(pItmLabel, CC_CALLBACK_1(ALayer::menEnterNextScene, this));
Menu* mn = Menu::create(pItmMenu, NULL);
mn->alignItemsVertically();
this->addChild(mn);
__NotificationCenter::getInstance()->addObserver(this, callfuncO_selector(ALayer::callBack), MSG_STATE, NULL);
return true;
}
// 进入下一个场景.
void ALayer::menEnterNextScene(cocos2d::Ref* pSender)
{
auto sc = BLayer::createScene();
auto reScene = TransitionSlideInR::create(1.0f, sc);
Director::getInstance()->pushScene(reScene);
}
void ALayer::callBack(cocos2d::Ref *sender)
{
log("ALayer callBack");
__String *str = (__String*)sender;
Label* label = (Label*)this->getChildByTag(100);
if (label)
label->setString(str->getCString());
}
ALayer::~ALayer()
{
__NotificationCenter::getInstance()->removeObserver(this, MSG_STATE);
}
#ifndef __BLayer_H__
#define __BLayer_H__
#include "cocos2d.h"
#define MSG_STATE "scene_notification"
class BLayer : public cocos2d::Layer
{
public:
static cocos2d::Scene* createScene();
virtual bool init();
// 更新单例对象状态.
void menUpdate(cocos2d::Ref* pSender);
// 返回上一个场景.
void menReturnPreviousScene(cocos2d::Ref* pSender);
// 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);
log("%s", s->getCString());
__NotificationCenter::getInstance()->postNotification(MSG_STATE, s);
}
void BLayer::menReturnPreviousScene(Ref* pSender)
{
Director::getInstance()->popScene();
}