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、使用效果



posted on 2017-07-19 08:15  lxjshuju  阅读(243)  评论(0编辑  收藏  举报