LayerMultiplex是层的控制器类

使用例如以下

LayerMultiplexTest.h

//
// LayerMultiplexTest.h
// cpp4
//
// Created by 杜甲 on 10/13/14.
//
// #ifndef __cpp4__LayerMultiplexTest__
#define __cpp4__LayerMultiplexTest__ #include "cocos2d.h" USING_NS_CC; class LayerMultiplexTest : public Layer
{
public: virtual bool init(); // there's no 'id' in cpp, so we recommend returning the class instance pointer
static cocos2d::Scene* scene(); // implement the "static node()" method manually
CREATE_FUNC(LayerMultiplexTest); }; class TestMainLayer : public LayerColor
{ public:
virtual bool init();
CREATE_FUNC(TestMainLayer); private:
void menuCallback1(cocos2d::Ref *sender);
void menuCallback2(cocos2d::Ref *sender); }; class TestLayer1 : public LayerColor
{ public:
virtual bool init();
CREATE_FUNC(TestLayer1);
void menuCallback1(cocos2d::Ref *sender);
}; class TestLayer2 : public LayerColor
{ public:
virtual bool init();
CREATE_FUNC(TestLayer2);
void menuCallback1(cocos2d::Ref *sender);
}; #endif /* defined(__cpp4__LayerMultiplexTest__) */

LayerMultiplexTest.cpp

//
// LayerMultiplexTest.cpp
// cpp4
//
// Created by 杜甲 on 10/13/14.
//
// #include "LayerMultiplexTest.h" Scene* LayerMultiplexTest::scene()
{
// 'scene' is an autorelease object
auto scene = Scene::create(); // 'layer' is an autorelease object
LayerMultiplexTest *layer = LayerMultiplexTest::create(); // add layer as a child to scene
scene->addChild(layer); // return the scene
return scene;
} bool LayerMultiplexTest::init()
{
bool bRet = false;
do {
CC_BREAK_IF(!Layer::init()); auto mainLayer = TestMainLayer::create();
auto layer1 = TestLayer1::create();
auto layer2 = TestLayer2::create();
//将层放入LayerMultiplex中
auto layerMutiplex = LayerMultiplex::create(mainLayer,layer1,layer2,nullptr);
addChild(layerMutiplex,0); bRet = true;
} while (0);
return bRet;
} bool TestMainLayer::init()
{
bool bRet = false;
do {
CC_BREAK_IF(!LayerColor::init());
auto winSize = Director::getInstance()->getWinSize(); auto label1 = Label::createWithBMFont("bitmapFontTest3.fnt", "TestLayer 1");
auto item1 = MenuItemLabel::create(label1, CC_CALLBACK_1(TestMainLayer::menuCallback1, this)); auto label2 = Label::createWithBMFont("bitmapFontTest3.fnt", "TestLayer 2");
auto item2 = MenuItemLabel::create(label2, CC_CALLBACK_1(TestMainLayer::menuCallback2, this)); auto menu = Menu::create(item1,item2, NULL);
menu->alignItemsVertically();
menu->setPosition(winSize / 2);
addChild(menu); bRet = true;
} while (0);
return bRet;
} void TestMainLayer::menuCallback1(cocos2d::Ref *sender)
{
static_cast<LayerMultiplex *>(_parent)->switchTo(1); } void TestMainLayer::menuCallback2(cocos2d::Ref *sender)
{
static_cast<LayerMultiplex *>(_parent)->switchTo(2);
} bool TestLayer1::init()
{
bool bRet = false;
do {
CC_BREAK_IF(!LayerColor::initWithColor(Color4B(100, 200, 200, 100)));
auto winSize = Director::getInstance()->getWinSize();
auto label1 = Label::createWithBMFont("bitmapFontTest3.fnt", "MainLayer");
auto item1 = MenuItemLabel::create(label1, CC_CALLBACK_1(TestLayer1::menuCallback1, this));
auto menu = Menu::create(item1, NULL);
menu->alignItemsVertically();
menu->setPosition(winSize / 2);
addChild(menu); bRet = true;
} while (0);
return bRet;
}
void TestLayer1::menuCallback1(cocos2d::Ref *sender)
{
//返回 方法
static_cast<LayerMultiplex *>(_parent)->switchTo(0); } bool TestLayer2::init()
{
bool bRet = false;
do {
CC_BREAK_IF(!LayerColor::initWithColor(Color4B(100, 100, 200, 100)));
auto winSize = Director::getInstance()->getWinSize();
auto label1 = Label::createWithBMFont("bitmapFontTest3.fnt", "MainLayer");
auto item1 = MenuItemLabel::create(label1, CC_CALLBACK_1(TestLayer2::menuCallback1, this)); auto menu = Menu::create(item1, NULL);
menu->alignItemsVertically();
menu->setPosition(winSize / 2);
addChild(menu); bRet = true;
} while (0);
return bRet;
} void TestLayer2::menuCallback1(cocos2d::Ref *sender)
{
static_cast<LayerMultiplex *>(_parent)->switchTo(0); }

代码下载:http://download.csdn.net/detail/qqmcy/8031733

最新文章

  1. xcode8.2 打包问题
  2. Java框架--jQueryEasyUI
  3. hdu 4045 2011北京赛区网络赛F 组合数+斯特林数 ***
  4. 用c++builder读取一个一行有多行变量的文件
  5. Qt入门(19)——自定义窗口部件
  6. ccf练习---节日
  7. WPF笔记(2.6 ViewBox)——Layout
  8. Hadoop 配置文件简介
  9. 改良UIScrollView滚动视图
  10. 数据库读写分离Amoeba
  11. 对es6中Promise和async的理解
  12. 线程&amp;进程&amp;协程
  13. iOS 11 导航栏 item 偏移问题 和 Swift 下 UIButton 设置 title、image 显示问题
  14. [TJOI 2010]中位数
  15. 可变字符串类 StringBuilder
  16. Storm介绍及安装部署
  17. 决策树算法原理(ID3,C4.5)
  18. Codeforces 520B:Two Buttons(思维,好题)
  19. 写个定时任务更新svn
  20. [转]Spring注解-@Configuration注解、@Bean注解以及配置自动扫描、bean作用域

热门文章

  1. a标签实现不跳转点击
  2. 前端读者 | 分分钟让你理解HTTPS
  3. (十)while和until循环
  4. [thinkphp] 获取根目录绝对路径
  5. HDU 5969 最大的位或【贪心/按位或/思维】
  6. 二叉树学习三:AVL树
  7. 2、Flask实战第2天:URL传参
  8. luogu P1126 机器人搬重物
  9. 【KMP】BZOJ3670-[Noi2014]动物园
  10. ListView控件(上)数据适配器:ListView绑定监听是SetOnItemClickListener