Cocos2dx:实现游戏关卡PageView方式(一)

PageView实现游戏关卡:实现类似android平台下的viewpager

#ifndef __LevelLayer_SCENE_H__
#define __LevelLayer_SCENE_H__

#include "cocos2d.h"
#include "ui/CocosGUI.h"
#include "LevelBaseLayer.h"


USING_NS_CC;
using namespace cocos2d;
using namespace ui;

class WorldBaseLayer;

class LevelLayer : public Layer
{
public:
    static cocos2d::Scene* createScene();
    
    LevelLayer();
    ~LevelLayer();
    
    virtual bool init() override;

    void pageViewEvent(Ref* pSender, PageViewEventType type);
    
    CREATE_FUNC(LevelLayer);
  

    
private:

};

#endif // __LevelLayer_SCENE_H__

#include "LevelLayer.h"

USING_NS_CC;

//关卡
LevelLayer::LevelLayer(){

};

LevelLayer::~LevelLayer(){


};

Scene* LevelLayer::createScene()
{
    // 'scene' is an autorelease object
    auto scene = Scene::create();
    
    // 'layer' is an autorelease object
    auto layer = LevelLayer::create();
    
    // add layer as a child to scene
    scene->addChild(layer);
    
    // return the scene
    return scene;
}


bool LevelLayer::init()
{
    if ( !Layer::init() )
    {
        return false;
    }
    
    scheduleUpdate();
    
    Size visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();
    Size winSize = Director::getInstance()->getWinSize();
    
    PageView *pageView=PageView::create();
    pageView->setTouchEnabled(true);
    pageView->setSize(Size(winSize.width, winSize.height));
    pageView->setPosition(Point(0,0));
    
    //创建4个page
    for (int i=0; i<4; i++) {
        Layout* layout = Layout::create();
        layout->setSize(Size(winSize.width, winSize.height));
        
        ImageView* imageView = ImageView::create(StringUtils::format("world_map.jpg"));
        imageView->setScale9Enabled(true);
        imageView->setSize(Size(400, 400));
        imageView->setPosition(Point(layout->getContentSize().width / 2.0f, layout->getContentSize().height / 2.0f));
        layout->addChild(imageView);
        
        Text* label = Text::create(StringUtils::format("page %d", i), "fonts/Marker Felt.ttf", 30);
        label->setColor(Color3B(192, 192, 192));
        label->setPosition(Point(layout->getContentSize().width / 2.0f, layout->getContentSize().height / 2.0f));
        layout->addChild(label);
        
        pageView->addPage(layout);
        
    }
    pageView->addEventListenerPageView(this, pagevieweventselector(LevelLayer::pageViewEvent));
    
    this->addChild(pageView);

    
    return true;
}


void LevelLayer::pageViewEvent(Ref* pSender, PageViewEventType type){
    
    switch (type) {
        case PAGEVIEW_EVENT_TURNING:
        {
            PageView* pageView = dynamic_cast<PageView*>(pSender);
            log("pageView->getCurPageIndex() = %ld",pageView->getCurrentPageIndex() + 1);
        }
            break;
            
            
        default:
            break;
    }
}







推荐使用这种方式,毕竟是原生的嘛,后文也会介绍另外一种方式。



阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ZBJDSBJ/article/details/51203293
个人分类: cocos2d
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭