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. Docker 组件如何协作?

    还记得我们运行的第一个容器吗?现在通过它来体会一下 Docker 各个组件是如何协作的. 容器启动过程如下: Docker 客户端执行 docker run 命令. Docker daemon 发现本 ...

  2. Django时区配置:有次发现缓存的时间总是有问题,原来是时区配置需要改

    # LANGUAGE_CODE = 'en-us' # TIME_ZONE = 'UTC' LANGUAGE_CODE = 'zh-Hans' TIME_ZONE = 'Asia/Shanghai'

  3. 视音频数据处理入门:RGB、YUV像素数据处理【转】

    转自:http://blog.csdn.net/leixiaohua1020/article/details/50534150 ==================================== ...

  4. unix网络编程第一章demo

    之前一直以为time_wait状态就是主动关闭的那一方产生.然后这个端口一直不可以用.实际我发现服务端监听一个端口.客户端发来连接后.传输数据后.服务端关闭客户端套接字后.用netstat -nat ...

  5. Django迁移数据库

    我们已经编写了博客数据库模型的代码,但那还只是 Python 代码而已,Django 还没有把它翻译成数据库语言,因此实际上这些数据库表还没有真正的在数据库中创建 为了让 Django 完成翻译,创建 ...

  6. 用python生成二维码

    Python生成二维码,可以使用qrcode模块, github地址 我是搬运工 首先安装, 因为打算生成好再展示出来,所以用到Pillow模块 pip install qrcode pip inst ...

  7. AC日记——【模板】二分图匹配 洛谷 P3386

    题目背景 二分图 题目描述 给定一个二分图,结点个数分别为n,m,边数为e,求二分图最大匹配数 输入输出格式 输入格式: 第一行,n,m,e 第二至e+1行,每行两个正整数u,v,表示u,v有一条连边 ...

  8. osstatus -9801 workerman websocket 小程序不带端口

    帮事业部的同事,解决问题,坑总结 小程序出现,osstatus -9801 情况好多,说一下配置环境可解决的方法和问题 tls 1.2, php 5.6+, nginx, workerman 做的 w ...

  9. LaTeX 如何在文档的侧面插入图片实现"绕排"?

    https://www.zhihu.com/question/26837705 https://blog.csdn.net/u012856866/article/details/70305834

  10. MySQL 几种调式分析利器

    目录 pstack gdb strace perf pstack 获取堆栈信息 问题线程的定位 负载较低 mysql_pid=4522 pstack $mysql_pid>pstack.info ...