Cocos2dx:PageView实现游戏关卡

前端之家收集整理的这篇文章主要介绍了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://www.f2er.com/cocos2dx/339809.html

猜你在找的Cocos2d-x相关文章