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 ...
随机推荐
- CodeforcesD. Aztec Catacombs
$n \leq 300000$的完全无向图,每条边有可行和不可行的状态,一开始只有$m \leq 300000$条边是可行的,给出.每次从$x$走到$y$时,所有与$x$相连的边的可行/不可行状态会改 ...
- 转 Kafka入门经典教程
Kafka入门经典教程 http://www.aboutyun.com/thread-12882-1-1.html 问题导读 1.Kafka独特设计在什么地方?2.Kafka如何搭建及创建topic. ...
- js 实现点击复制文本内容
js 实现点击复制文本内容 <table> <tr><td>姓名:<span onclick="copyContent(this);" ...
- yii执行流程
yii执行流程 原文:http://www.cnblogs.com/bluecobra/archive/2011/11/30/2269207.html 一 目录文件 |-framework 框 ...
- AC日记——Propagating tree Codeforces 383c
C. Propagating tree time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- OceanBase分区表有什么不同?
概述 分区表是ORACLE从8.0开始引入的功能,也是第一个支持物理分区的数据库,随后其他数据库纷纷跟进.分区表是一种“分而治之”的思想,通过将大表.索引分成可以独立管理的.小的片段(Segment) ...
- 布斯(Steve Jobs)在斯坦福大学的演讲稿,中英文对照版
2005年6月14日,苹果CEO史蒂夫·乔布斯(Steve Jobs)在他的母校斯坦福大学的毕业典礼发表了著名的演讲,关于这段演讲,你会看到N多人的推荐(比如同样喜欢在大学演讲的李开复先生).此前曾经 ...
- java 图片加水印,设置透明度。说明非常具体
package com.yidao.common; import java.awt.AlphaComposite; import java.awt.Graphics2D; import java.aw ...
- 深入理解Java中的HashMap的实现原理
HashMap继承自抽象类AbstractMap,抽象类AbstractMap实现了Map接口.关系图例如以下所看到的: Java中的Map<key, value>接口同意我们将一个对象作 ...
- Nutch学习笔记一 ---环境搭建
学习环境: ubuntu 概要: Nutch 是一个开源Java 实现的搜索引擎.它提供了我们运行自己的搜索引擎所需的全部工具.包括全文搜索和Web爬虫. 通过nutch,诞生了hadoop.tika ...