Cocos2d-x之LayerMultiplex的使用
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的使用的更多相关文章
- Cocos2d-x3.2 LayerMultiplex使用说明
LayerMultiplex是层的控制器类 使用例如以下 LayerMultiplexTest.h // // LayerMultiplexTest.h // cpp4 // // Created b ...
- 小尝试一下 cocos2d
好奇 cocos2d 到底是怎样一个框架,正好有个项目需要一个游戏框架,所以稍微了解了一下.小结一下了解到的情况. 基本概念 首先呢,因为 cocos2d 是基于 pyglet 做的,你完全可以直接用 ...
- 采用cocos2d-x lua 制作数字滚动效果样例
require "Cocos2d"require "Cocos2dConstants"local testscene = class("testsce ...
- Cocos2d 利用继承Draw方法制作可显示三维数据(宠物三维等)的三角形显示面板
很久没有写博客了,这段时间比较忙,又是搬家又是做自己的项目,还有太多琐碎的事情缠身,好不容易抽出时间把最近自己做的一些简单例子记录一下. 在我的项目中,我需要一个显示面板来显示游戏中的一个三维数据,例 ...
- iPhone开发与cocos2d 经验谈
转CSDN jilongliang : 首先,对于一个完全没有mac开发经验,甚至从没摸过苹果系统的开发人员来说,首先就是要熟悉apple的那一套开发框架(含开发环境IDE.开发框架uikit,还有开 ...
- cocos2d学习记录
视频 - http://www.manew.com/forum-105-3.html一个论坛帖 - http://www.zhihu.com/question/21114802官网 - http:// ...
- Android下Cocos2d创建HelloWorld工程
最近在搭建Cocos2d的环境,结果各种问题,两人弄了一天才能搞好一个环境-! -_-!! 避免大家也可能会遇到我这种情况,所以写一个随笔,让大家也了解下如何搭建吧- 1.环境安装准备 下载 tadp ...
- 学生信息管理系统(cocos2d引擎)——数据结构课程设计
老师手把手教了两天半,看了一下模式,加了几个功能就大功告成了!!! 给我的感想就是全都是指针! 添加图片精灵: CCSprite* spBG = CCSprite::create("&qu ...
- cocos2d触碰例子代码
// // TestLayer.h // MiniTD // // Created by OnePiece on 12-7-30. // Copyright 2012年 __MyCompanyName ...
随机推荐
- vue.js源码学习分享(九)
/* */ var arrayKeys = Object.getOwnPropertyNames(arrayMethods);//获取arrayMethods的属性名称 /** * By defaul ...
- 标准C程序设计七---47
Linux应用 编程深入 语言编程 标准C程序设计七---经典C11程序设计 以下内容为阅读: <标准C程序设计>(第7版) 作者 ...
- iOS8下定位问题解决
项目是以前iOS7的,升级iOS8后,无法成功获取用户位置.后来才发现iOS8 使用定位需要在infoplist文件中加2个key,然后manager需要加一个方法,指定定位授权机制 在plist ...
- Scrapy学习-15-降低被识别为爬虫的方法
3种常见的方法 1. 在settings中配置禁用cookies COOKIES_ENABLED = False 2. scrapy限速处理,scrapy为我们提供了扩展模块,它能动态的限制下载速度 ...
- fetch格式
fetch('url'+参数a, { method: "GET", body: json } .then(res => response.json()) .then(cons ...
- 微信小程序 使用微信支付功能实现在线订单支付
以前做过PC页面微信支付,但是这次在小程序 直接调用微信支付功能还是方便很多 先放个微信官方API链接:https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_a ...
- 《Python基础教程读书笔记》
第1章 快速构造:基础知识 1.2交互式构造器 不强制分号,一行就是一行.可以加上分号 1.4数字和表达式 加.减.乘.除.整除.求余.指数.取反(-例如-2**2,**的优先级比-大) from _ ...
- Oracle PL/SQL 存储过程、函数、包 的范例
1,写函数和过程,输入三角形三个表的长度.在控制台打印三角形的面积 -- 创建包 create or replace package pac_area is -- 定义计算三角形面积的过程 proce ...
- IntelliJ IDEA中Spring Boot项目使用spring-boot-devtools无法实现热部署/热更新的问题解决
这个设置真的和Eclipse有很大区别,Eclipse中只要运行之后就可实现修改文件自动重启.但IDEA不太一样,需要做如下配置: 前提: 1.添加spring-boot-devtools到POM. ...
- win10 sublime license
—– BEGIN LICENSE —– TwitterInc 200 User License EA7E-890007 1D77F72E 390CDD93 4DCBA022 FAF60790 61AA ...