Cocos2d-x3.2 LayerMultiplex使用说明

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(_parent)->switchTo(1); } void TestMainLayer::menuCallback2(cocos2d::Ref *sender) { static_cast(_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(_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(_parent)->switchTo(0); }

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

点击复制链接 与好友分享!回本站首页
您对本文章有什么意见或着疑问吗?请到论坛讨论您的关注和建议是我们前行的参考和动力
上一篇:cocos2d-x 粒子效果
下一篇:Cocos2d-x V3.2+Cocos Studio1.6 实现一个简单的uibutton点击功能
相关文章
图文推荐

Cocos2d-x3.2 LayerMultiplex使用说明
quick-cocos2d-x 学
Cocos2d-x3.2 LayerMultiplex使用说明
Cocos2d-X实现七夕情
Cocos2d-x3.2 LayerMultiplex使用说明
Cocos2d-x扣血飘字特
Cocos2d-x3.2 LayerMultiplex使用说明
Cocos2d-x 3.2 大富
分类:默认分类 时间:2015-03-12 人气:1
本文关键词:
分享到:

相关文章

Copyright (C) quwantang.com, All Rights Reserved.

趣玩堂 版权所有 京ICP备15002868号

processed in 0.034 (s). 10 q(s)