Cocos2d-x之LayerMultiplex的使用,layermultiplex

來源:互聯網
上載者:User

Cocos2d-x之LayerMultiplex的使用,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中在最上層添加一個新的Layer, 怎把其他層的觸摸全部屏蔽

應該你可以設定這個layer的TouchHandle的swallowsTouches為YES,這樣就吞下了這個touch,其他的層就不再響應了,就CCMenuItem一樣的處理方法,你可以試一下。
希望能協助到你。
 
cocos2d-x在helloworld層上建了兩個層layer1與layer2兩個層都設定了tableview對兩個層監聽,

你把兩個層錯位一點,分別給它們取一個名字,利用名字獲得。。。
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.