zoukankan      html  css  js  c++  java
  • 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、使用效果



  • 相关阅读:
    APP开发收藏的几个网址,APP性能监测
    MAC 安装STF
    更新react 之后 出现 can not find ‘@babel/runtime/helpers/esm/createSuper’ 提示
    react 相关笔记
    jenkins 配置子节点 关键在端口号和下方代理配置
    移动APP测试8点注意事项
    自动部署java项目,热部署方式
    ubuntu搭建elk服务器
    数据库常用操作,sql server; mysql
    windows 关闭端口占用及其他常见操作
  • 原文地址:https://www.cnblogs.com/lxjshuju/p/7203784.html
Copyright © 2011-2022 走看看