Use of LayerMultiplex in Cocos2d-x

Source: Internet
Author: User

Use of LayerMultiplex in Cocos2d-x
1. Usage

It is used to manage Layer switching without switching scenarios.
2. Code 1). h file
#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 File
#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
 
  (_parent)->switchTo(1);}void MainLayer::menuCallback2(cocos2d::Ref *sender){    static_cast
  
   (_parent)->switchTo(2);}void MainLayer::menuCallback3(cocos2d::Ref *sender){    static_cast
   
    (_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
    
     (_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
     
      (_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
      
       (_parent)->switchTo(0);}
      
     
    
   
  
 
3. Effects

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.