【转载】cocos2d-x3.0 关于ScrollView的使用
本文主要介绍下ScrollView在cocos2dx中的使用,其主要用来实现关卡的选择,以及人物拖拽选择等方面。比如:
人物选择:
关卡选择:
其他不多说直接进入主题:
.h文件
#include "cocos2d.h"
#include "cocos-ext.h"
USING_NS_CC_EXT;
USING_NS_CC;
使用scrollview要引入头文件
然后继承ScrollViewDelegate
class HelloWorld :public Layer,publicScrollViewDelegate
{
public:
static Scene* createScene();
virtual bool init();
void menuCloseCallback(Object* pSender);
CREATE_FUNC(HelloWorld);
private:
//ScrollView中必须实现的2个虚函数方法
virtual void scrollViewDidScroll(ScrollView* view);
virtual void scrollViewDidZoom(ScrollView* view);
Node* create9Sprite(float pWith,float pHeight);//创建9妹精灵
bool onTouchBegan(Touch* touch,Event* event);//触摸开始方法
void onTouchMoved(Touch* touch,Event* event);//触摸移动方法
void onTouchEnded(Touch* touch,Event* event);//触摸结束方法
private:
int x; //scrollView的当前横坐标
ScrollView *scrollView;
};
.cpp
USING_NS_CC;
#define NUM 10
Scene*HelloWorld::createScene()
{
auto scene = Scene::create();
auto layer = HelloWorld::create();
scene->addChild(layer);
return scene;
}
Node*HelloWorld::create9Sprite(float pWith,float pHeight)
{
auto sprite9 = Scale9Sprite::create("button.png");
LabelTTF *label = LabelTTF::create("Demo","",50);
//创建按钮cb
ControlButton* cb =ControlButton::create(sprite9);
//setPreferredSize 就是设置需要生成的尺寸大小
/*强制设置按钮大小,如果按钮标题大小超过这个范围,则会自动扩大 */
cb->setPreferredSize(Size(190,190));
cb->setPosition(Point(pWith, pHeight));
return cb;
}
bool HelloWorld::init()
{
if (!Layer::init()){returnfalse;}
Size screenSize=Director::getInstance()->getVisibleSize();
Sprite *bg=Sprite::create("HelloWorld.png");
bg->setPosition(screenSize.width/2,screenSize.height/2);
bg->setOpacity(125);
addChild(bg,2);
//设置容器
Layer* continerLayer = Layer::create();
continerLayer->setContentSize(Size(NUM*200,200));//设置滚动区域的大小
continerLayer->setPosition(Point(0,0));
//添加button
for (int i =0; i<NUM; i++)
{
continerLayer->addChild(this->create9Sprite(i*200+100,100));
}
scrollView = ScrollView::create();
scrollView->setViewSize(Size(600,200));//设置滚动试图的试图大小
scrollView->setPosition(150,210);//设置位置 新的层默认左下角为原点
scrollView->setContainer(continerLayer);//添加容器
scrollView->setDirection(ScrollView::Direction::HORIZONTAL);//设置滑动方向
scrollView->setDelegate(this);//注册
this->addChild(scrollView);//添加
//创建屏幕监听事件
auto listener =EventListenerTouchOneByOne::create();
//设置可向下传递
listener->setSwallowTouches(true);
listener->onTouchBegan =CC_CALLBACK_2(HelloWorld::onTouchBegan,this);
listener->onTouchMoved =CC_CALLBACK_2(HelloWorld::onTouchMoved,this);
listener->onTouchEnded =CC_CALLBACK_2(HelloWorld::onTouchEnded,this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener,this);
return true;
}
boolHelloWorld::onTouchBegan(Touch* touch,Event* event)
{
return true;
}
voidHelloWorld::onTouchMoved(Touch* touch,Event* event)
{
x =scrollView->getContentOffset().x;//x轴的偏移量
CCLOG("%d",x);
}
voidHelloWorld::onTouchEnded(Touch* touch,Event* event)
{
// 关闭CCScrollView中的自调整
scrollView->unscheduleAllSelectors();
//矫正view的位置
int offset = x %200;
int num = x /200;
Point adjustPos;
float adjustAnimDelay;//慢慢返回中间的位置需要的时间
//图片在几种特殊情况下的处理
if (offset<=-100) {
adjustPos =Point((num-1)*200,scrollView->getContentOffset().y);
adjustAnimDelay=(offset*(-1)%100)/600;
}
if (offset<=0&&offset>-100) {
adjustPos =Point(num*200,scrollView->getContentOffset().y);
adjustAnimDelay=(offset*(-1)%100)/600;
}
if (offset>0) {
adjustPos =Point(0,scrollView->getContentOffset().y);
adjustAnimDelay=(offset*(1)%100)/600;
}
if (x<=-1400) {
adjustPos =Point(-1400,scrollView->getContentOffset().y);
adjustAnimDelay=(offset*(-1)%100)/600;
}
adjustAnimDelay=0.7;
scrollView->setContentOffsetInDuration(adjustPos, adjustAnimDelay);// 调整位置
}
voidHelloWorld::scrollViewDidScroll(ScrollView* view)
{
return;
}
voidHelloWorld::scrollViewDidZoom(ScrollView* view)
{
return;
}
不太会用这写东西 大家将就看吧!