cocos2d-x 3.x pageView的使用

我们要做的效果和捕鱼达人关卡选择效果是一样的,我们先看下做好的效果:



实现滑动并不难,在这里我们需要用到pageView类,它跟我们在前面一篇写的进度条LoadingBar类一样都是属于CocosGUI里的控件工具类,所以在用他们之前必须先包含头文件和命名空间。

首先看头文件

#ifndef _MYGAME_H_
#define _MYGAME_H_
#include "cocos2d.h"
#include "ui/CocosGUI.h"
USING_NS_CC;
using namespace ui;

#define		pWidth		240
#define		pHeight		160
class MyGame : public Layer
{
public:
	//静态方法创建一个场景
	static Scene* createScene();
	//初始化
	virtual bool init();
	//回调函数
	void JumpSceneCallback(Ref* pScene);
	//create函数
	CREATE_FUNC(MyGame);
	virtual bool onTouchBegan(Touch *touch, Event *unused_event);
	virtual void onTouchEnded(Touch * touch, Event * unused_event);
	void initRole();
	static Scene* createS();
	void initS();
privated:
	PageView*				pageView;
	Vec2					ClickBeganPos;  //点击的坐标
};
在看cpp文件实现

bool MyGame::init()
{
if(!Layer::init())
{return false;}
//触摸事件监听
auto lisent = EventListenerTouchOneByOne::create();
	lisent->onTouchBegan = CC_CALLBACK_2(MyGame::onTouchBegan, this);
	lisent->onTouchEnded = CC_CALLBACK_2(MyGame::onTouchEnded, this);
	Director::getInstance()->getEventDispatcher()->
		addEventListenerWithSceneGraphPriority(lisent, this);

	//滑动关卡
	Size WinSize = Director::getInstance()->getWinSize();

	pageView = PageView::create();
	pageView->setSize(Size(WinSize.width, WinSize.height));
	pageView->setPosition(Vec2::ZERO);
//8个滑动选项卡
	for (int i = 0; i < 8;i++)
	{
		auto layout = Layout::create();
		layout->setSize(Size(WinSize.width, WinSize.height));

		auto imageView = ImageView::create("bug886.png");
		imageView->setSize(Size(pWidth, pHeight));
		imageView->setPosition(Vec2(layout->getSize().width / 2, layout->getSize().height / 2));
		layout->addChild(imageView);

		auto label = Label::create(StringUtils::format("Level 0%d", i), "Arial", 46);
		label->setColor(Color3B(192, 192, 192));
		label->setPosition(Vec2(layout->getSize().width / 2, layout->getSize().height / 2));
		layout->addChild(label);

		pageView->addPage(layout);

	}
//滑动监听
	pageView->addEventListenerPageView(this, pagevieweventselector(MyGame::JumpSceneCallback));

	this->addChild(pageView);
        pageView->scrollToPage(3);//默认显示指定项

 
  
	return true;
}
bool MyGame::onTouchBegan(Touch *touch, Event *unused_event)
{
	return true;
}

void MyGame::onTouchEnded(Touch * touch, Event * unused_event)
{

}

void MyGame::JumpSceneCallback(Ref* pScene)
{}
具体触摸的以及回调函数实现什么功能就自己根据自己需要相应的添加。


你可能感兴趣的:(cocos2d,3.x学习笔记)