Cocos2d-x之LayerMultiplex的使用

Cocos2d-x之LayerMultiplex的使用,第1张

概述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: CR 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,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,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-x之LayerMultiplex的使用所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1003257.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-22
下一篇2022-05-22

发表评论

登录后才能评论

评论列表(0条)

    保存