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 ...
随机推荐
- hust 1605 - Gene recombination(bfs+字典树)
1605 - Gene recombination Time Limit: 2s Memory Limit: 64MB Submissions: 264 Solved: 46 DESCRIPTION ...
- 更改 terminal 開啟時,預設的路徑
echo "cd /media" >> ~/.bashrc open a new terminal (ctrl + alt + t)
- (1)JavaScript基础1
一.javaScript 由三部分组成 1.核心(ECMAScript) 2.文档对象模型(DOM) 3.浏览器对象模型(BOM) 二.在html中使用javascript HTML5模板 <! ...
- Tyvj——P1864 [Poetize I]守卫者的挑战
来源:http://www.tyvj.cn/p/1864 描述 打开了黑魔法师Vani的大门,队员们在迷宫般的路上漫无目的地搜寻着关押applepi的监狱的所在地.突然,眼前一道亮光闪过.“我,Niz ...
- Network | sk_buff
sk_buff结构可能是linux网络代码中最重要的数据结构,它表示接收或发送数据包的包头信息.它在中定义,并包含很多成员变量供网络代码中的各子系统使用. 这个结构被不同的网络层(MAC或者其他 ...
- Xamarin.Forms XAML控件的公共属性
Xamarin.Forms XAML控件的公共属性 Xamarin.Forms XAML控件有很多.通过官网API,可以查看每个控件的属性.但是官网只给出了控件的特有属性,而公共属性没有列出.所以 ...
- TreeSet, LinkedHashSet and HashSet 的区别
1. 介绍 TreeSet, LinkedHashSet and HashSet 在java中都是实现Set的数据结构 # TreeSet的主要功能用于排序 # LinkedHashSet的主要功能用 ...
- atom 隐藏右边的白线
atom-text-editor.editor .wrap-guide {//隐藏右边的白线visibility: hidden;}
- openfire Android学习(一)----实现用户注册、登录、修改密码和注销等
以前学习过用Scoket 建立聊天,简单的建立聊天是没问题的,但如果要实现多人复杂的聊天,后台服务器代码就比较复杂,对于我这新手来讲就比较难了.后来在网上看到用openfire做服务器,利用强大的Sm ...
- C# Http方式下载文件到本地
下文代码是从网络(http://www.cnblogs.com/hayden/archive/2012/04/26/2472815.html)得来,亲测好用.我中修改了下格式和注释,版权属于原作者“舒 ...