1、用处

用于管理Layer的切换,而不用切换场景。

2、代码

1).h文件

#include "cocos2d.h"
#include "ui/CocosGUI.h"
#include "VisibleRect.h"
USING_NS_CC;
using namespace ui; class LayerMultiplexDemo : public Scene
{
public:
CREATE_FUNC(LayerMultiplexDemo);
virtual bool init();
}; class BaseLayer : public Layer
{
public:
CREATE_FUNC(BaseLayer);
virtual bool init();
Text* text;
Size winSize;
}; class MainLayer : public BaseLayer
{
public:
CREATE_FUNC(MainLayer);
virtual bool init(); void menuCallback1(Ref* sender);
void menuCallback2(Ref* sender);
void menuCallback3(Ref* sender);
}; class Layer1 : public BaseLayer
{
public:
CREATE_FUNC(Layer1);
virtual bool init();
void touchEvent(cocos2d::Ref *pSender, cocos2d::ui::Widget::TouchEventType type);
}; class Layer2 : public BaseLayer
{
public:
CREATE_FUNC(Layer2);
virtual bool init();
void touchEvent(cocos2d::Ref *pSender, cocos2d::ui::Widget::TouchEventType type);
}; class Layer3 : public BaseLayer
{
public:
CREATE_FUNC(Layer3);
virtual bool init();
void touchEvent(cocos2d::Ref *pSender, cocos2d::ui::Widget::TouchEventType type);
};

2).cpp文件

#include "LayerMultiplexDemo.h"
bool LayerMultiplexDemo::init()
{
bool bRet = false;
do{
CC_BREAK_IF(!Scene::init()); MenuItemFont::setFontSize(20); auto layer = MainLayer::create();
auto layer1 = Layer1::create();
auto layer2 = Layer2::create();
auto layer3 = Layer3::create(); auto layerMultiplex = LayerMultiplex::create(layer,layer1, layer2, layer3, nullptr);
addChild(layerMultiplex, 0); bRet = true;
}while(0);
return bRet;
} bool BaseLayer::init()
{
bool bRet = false;
do{
CC_BREAK_IF(!Layer::init());
winSize = Director::getInstance()->getWinSize(); text = Text::create();
text->setFontSize(40);
text->setPosition(Vec2(winSize.width/2,winSize.height - 100));
addChild(text); bRet = true;
}while(0);
return bRet;
} bool MainLayer::init()
{
bool bRet = false;
do{
CC_BREAK_IF(!BaseLayer::init());
text->setString("Hello! This is MainLayer!"); auto label1 = Label::createWithBMFont("bitmapFontTest3.fnt", "Layer 1");
auto item1 = MenuItemLabel::create(label1, CC_CALLBACK_1(MainLayer::menuCallback1, this)); auto label2 = Label::createWithBMFont("bitmapFontTest3.fnt", "Layer 2");
auto item2 = MenuItemLabel::create(label2, CC_CALLBACK_1(MainLayer::menuCallback2, this)); auto label3 = Label::createWithBMFont("bitmapFontTest3.fnt", "Layer 3");
auto item3 = MenuItemLabel::create(label3, CC_CALLBACK_1(MainLayer::menuCallback3, this)); auto menu = Menu::create(item1,item2,item3,nullptr);
menu->alignItemsVertically();
addChild(menu);
menu->setPosition(Vec2(winSize.width/2,winSize.height/2)); bRet = true;
}while(0);
return bRet;
} void MainLayer::menuCallback1(cocos2d::Ref *sender)
{
static_cast<LayerMultiplex*>(_parent)->switchTo(1);
} void MainLayer::menuCallback2(cocos2d::Ref *sender)
{
static_cast<LayerMultiplex*>(_parent)->switchTo(2);
}
void MainLayer::menuCallback3(cocos2d::Ref *sender)
{
static_cast<LayerMultiplex*>(_parent)->switchTo(3);
} bool Layer1::init()
{
bool bRet = false;
do{
CC_BREAK_IF(!BaseLayer::init()); text->setString("Hello! This is Layer1"); auto layout = Layout::create();
layout->setContentSize(Size(300,300));
layout->setBackGroundColorType(cocos2d::ui::Layout::BackGroundColorType::SOLID);
layout->setBackGroundColor(Color3B::GRAY);
layout->ignoreAnchorPointForPosition(false);
layout->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
layout->setPosition(Vec2(winSize.width/2,winSize.height/2));
addChild(layout);
auto button = Button::create("btn-about-normal.png","btn-about-selected.png");
button->setPosition(Vec2(layout->getContentSize().width/2,layout->getContentSize().height/2));
layout->addChild(button);
button->addTouchEventListener(CC_CALLBACK_2(Layer1::touchEvent, this)); bRet = true;
}while(0);
return bRet;
}
void Layer1::touchEvent(cocos2d::Ref *pSender, cocos2d::ui::Widget::TouchEventType type)
{
static_cast<LayerMultiplex*>(_parent)->switchTo(0);
} bool Layer2::init()
{
bool bRet = false;
do{
CC_BREAK_IF(!BaseLayer::init());
text->setString("Hello! This is Layer2"); auto layout = Layout::create();
layout->setContentSize(Size(300,300));
layout->setBackGroundColorType(cocos2d::ui::Layout::BackGroundColorType::SOLID);
layout->setBackGroundColor(Color3B::GRAY);
layout->ignoreAnchorPointForPosition(false);
layout->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
layout->setPosition(Vec2(winSize.width/2,winSize.height/2));
addChild(layout);
auto button = Button::create("btn-about-normal.png","btn-about-selected.png");
button->setPosition(Vec2(layout->getContentSize().width/2,layout->getContentSize().height/2));
layout->addChild(button);
button->addTouchEventListener(CC_CALLBACK_2(Layer2::touchEvent, this)); bRet = true;
}while(0);
return bRet;
}
void Layer2::touchEvent(cocos2d::Ref *pSender, cocos2d::ui::Widget::TouchEventType type)
{
static_cast<LayerMultiplex*>(_parent)->switchTo(0);
} bool Layer3::init()
{
bool bRet = false;
do{
CC_BREAK_IF(!BaseLayer::init());
text->setString("Hello! This is Layer3"); auto layout = Layout::create();
layout->setContentSize(Size(300,300));
layout->setBackGroundColorType(cocos2d::ui::Layout::BackGroundColorType::SOLID);
layout->setBackGroundColor(Color3B::GRAY);
layout->ignoreAnchorPointForPosition(false);
layout->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
layout->setPosition(Vec2(winSize.width/2,winSize.height/2));
addChild(layout);
auto button = Button::create("btn-about-normal.png","btn-about-selected.png");
button->setPosition(Vec2(layout->getContentSize().width/2,layout->getContentSize().height/2));
layout->addChild(button);
button->addTouchEventListener(CC_CALLBACK_2(Layer3::touchEvent, this)); bRet = true;
}while(0);
return bRet;
}
void Layer3::touchEvent(cocos2d::Ref *pSender, cocos2d::ui::Widget::TouchEventType type)
{
static_cast<LayerMultiplex*>(_parent)->switchTo(0);
}

3、使用效果


Cocos2d-x之LayerMultiplex的使用的更多相关文章

  1. Cocos2d-x3.2 LayerMultiplex使用说明

    LayerMultiplex是层的控制器类 使用例如以下 LayerMultiplexTest.h // // LayerMultiplexTest.h // cpp4 // // Created b ...

  2. 小尝试一下 cocos2d

    好奇 cocos2d 到底是怎样一个框架,正好有个项目需要一个游戏框架,所以稍微了解了一下.小结一下了解到的情况. 基本概念 首先呢,因为 cocos2d 是基于 pyglet 做的,你完全可以直接用 ...

  3. 采用cocos2d-x lua 制作数字滚动效果样例

    require "Cocos2d"require "Cocos2dConstants"local testscene = class("testsce ...

  4. Cocos2d 利用继承Draw方法制作可显示三维数据(宠物三维等)的三角形显示面板

    很久没有写博客了,这段时间比较忙,又是搬家又是做自己的项目,还有太多琐碎的事情缠身,好不容易抽出时间把最近自己做的一些简单例子记录一下. 在我的项目中,我需要一个显示面板来显示游戏中的一个三维数据,例 ...

  5. iPhone开发与cocos2d 经验谈

    转CSDN jilongliang : 首先,对于一个完全没有mac开发经验,甚至从没摸过苹果系统的开发人员来说,首先就是要熟悉apple的那一套开发框架(含开发环境IDE.开发框架uikit,还有开 ...

  6. cocos2d学习记录

    视频 - http://www.manew.com/forum-105-3.html一个论坛帖 - http://www.zhihu.com/question/21114802官网 - http:// ...

  7. Android下Cocos2d创建HelloWorld工程

    最近在搭建Cocos2d的环境,结果各种问题,两人弄了一天才能搞好一个环境-! -_-!! 避免大家也可能会遇到我这种情况,所以写一个随笔,让大家也了解下如何搭建吧- 1.环境安装准备 下载 tadp ...

  8. 学生信息管理系统(cocos2d引擎)——数据结构课程设计

    老师手把手教了两天半,看了一下模式,加了几个功能就大功告成了!!! 给我的感想就是全都是指针! 添加图片精灵: CCSprite*  spBG = CCSprite::create("&qu ...

  9. cocos2d触碰例子代码

    // // TestLayer.h // MiniTD // // Created by OnePiece on 12-7-30. // Copyright 2012年 __MyCompanyName ...

随机推荐

  1. UVa11762 Race to 1

    期望DP 一个数只能分解成不大于它的数,那么转移构成拓扑关系. 试了一下预处理出不大于x的质数个数,然而程序并没有变快 /*by SilverN*/ #include<algorithm> ...

  2. 洛谷P1201 [USACO1.1]贪婪的送礼者Greedy Gift Givers

    题目描述 对于一群(NP个)要互送礼物的朋友,GY要确定每个人送出的钱比收到的多多少.在这一个问题中,每个人都准备了一些钱来送礼物,而这些钱将会被平均分给那些将收到他的礼物的人.然而,在任何一群朋友中 ...

  3. Codeforces878E. Numbers on the blackboard

    $n \leq 100000$的数列,数字范围$-1e9,1e9$,现$q \leq 1e5$个每次问在一个区间玩游戏,能得到的最大的数.“游戏”:选相邻两个数$a_x,a_y$,然后把他们删掉,变成 ...

  4. CSS 盒模型、解决方案、BFC 原理讲解--摘抄

    PS:内容比较基础,目的只是覆盖面试知识点,大佬可以 history.back(-1) W3C 标准盒模型 & IE 怪异盒模型 页面上显示的每个元素(包括内联元素)都可以看作一个盒子,即盒模 ...

  5. 转 使用putty从linux主机上面往windows主机下面拷贝文件

    更新一下,把putty的包解压以后,想要在dos窗口中直接使用,必须把putty解压的文件的路径添加到环境变量中,这样使用起来就会非常简单了. 郁闷了好久,终于搞定了putty的上传下载文件命令psc ...

  6. 标准C程序设计七---52

    Linux应用             编程深入            语言编程 标准C程序设计七---经典C11程序设计    以下内容为阅读:    <标准C程序设计>(第7版) 作者 ...

  7. 45深入理解C指针之---指针释放

    一.size_t:用于安全表示长度,所有平台和系统都会解析成自己对应的长度 1.定义:size_t类型表示C中任何对象所能表示的最大长度,是个无符号整数:常常定义在stdio.h或stdlib.h中 ...

  8. Linux内核之网络

    应用层: 套接字将Unix一切都是内核的概念应用到网络连接中,内核跟用户空间套接字之间的接口实现在c的标准库中,使用了socketcall系统调用. socketcall充当了一个多路分解器,将各种任 ...

  9. hdu 1065(推公式)

    I Think I Need a Houseboat Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Ja ...

  10. js中加“var”和不加“var”的区别,看完觉得这么多年js白学了

    Javascript声明变量的时候,虽然用var关键字声明和不用关键字声明,很多时候运行并没有问题,但是这两种方式还是有区别的.可以正常运行的代码并不代表是合适的代码. var num = 1: 是在 ...