#pragma once
#include "cocos2d.h"
#include "ShopScene.h"
using namespace cocos2d;
class ChooseScene : public Layer
{
public:
static Scene* createScene();
virtual bool init();
void menuCloseCallback(Ref* pSender);CREATE_FUNC(ChooseScene);
public:
bool onTouchBegan(Touch* touch, Event* event);
void onTouchMoved(Touch* touch, Event* event);
void onTouchEnded(Touch* touch, Event* event);
Point diff_2;
Point diff_1;
float bg_width_max;
float bg_width_min;
Sprite* cocosImage;
};
cpp
#include "ChooseScene.h"
USING_NS_CC;
Scene* ChooseScene::createScene()
{
auto scene = Scene::create();
auto layer = ChooseScene::create();
scene->addChild(layer);return scene;
}
bool ChooseScene::init()
{
if ( !Layer::init() )
{return false;}
Size visibleSize = Director::getInstance()->getVisibleSize();
Point origin = Director::getInstance()->getVisibleOrigin();
bg_width_max = 6500; // 背景完全出现 最大X
bg_width_min = 1024; // 最小X
background_init();
return true;
}
void ChooseScene::background_init()
{
cocosImage = Sprite::create("ChooseScene/map.png");
cocosImage->setAnchorPoint(Point(1, 0)); ///锚点 1,0 图片的右下角
cocosImage->setPosition(Point(1024, 0)); // 屏幕是 1024 768 锚点是1,0 所以全部显示在屏幕上 x 位置 是1024
this->addChild(cocosImage, 1, 1);
auto listener_1 = EventListenerTouchOneByOne::create(); //添加监听事件
listener_1->setSwallowTouches(true);
listener_1->onTouchBegan = CC_CALLBACK_2(ChooseScene::onTouchBegan, this);
listener_1->onTouchMoved = CC_CALLBACK_2(ChooseScene::onTouchMoved,this);
listener_1->onTouchEnded = CC_CALLBACK_2(ChooseScene::onTouchEnded,this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener_1, this);
}
bool ChooseScene::onTouchBegan(Touch* touch, Event* event) // 如果有触摸范围的话 可以再这里添加一个
{
// auto beginPoint = touch->getLocation();
// if (touch_bg.containsPoint(beginPoint)) /touch_bg 是point
// {
// return true;
// }
return true;
}
void ChooseScene::onTouchMoved(Touch* touch, Event* event)
{
if (cocosImage->getPositionX() >= bg_width_min && cocosImage->getPositionX() <= bg_width_max) //判断 是不是在 规定的范围内
{
diff_2 = touch->getDelta();
auto currentPos = cocosImage->getPosition();
cocosImage->setPosition(currentPos.x + diff_2.x, currentPos.y);
}
if (cocosImage->getPositionX() > bg_width_max) // 最左边 //当移动到最左边的时候 返回最大范围
{
cocosImage->setPosition(bg_width_max, cocosImage->getPositionY());
}
if (cocosImage->getPositionX() < bg_width_min) /// 最右边 同上
{
cocosImage->setPosition(bg_width_min, cocosImage->getPositionY());
}
}
void ChooseScene::onTouchEnded(Touch* touch, Event* event)
{
if (diff_1.x == diff_2.x) //判断 上次触摸点与这次触摸点 是否不同 如果相同 地图不进行移动
{
return;
}
diff_1 = diff_2; //保留这次移动参数
float pm = 0;
if (diff_1.y > 0)
{
pm = 1;
}
else
{
pm = -1;
}
if (cocosImage->getPositionX() >= bg_width_min - pm * 180
&& cocosImage->getPositionX() <= bg_width_max - pm * 180) /// 滑动边界判断 最大范围
{
if (abs(diff_1.x)>=30)
{
if (diff_1.x >0)
{
auto move_x = MoveTo::create(0.8f, Point(cocosImage->getPositionX() + 180,
cocosImage->getPositionY()));
cocosImage->runAction(move_x);
}
else
{
auto move_x = MoveTo::create(0.8f, Point(cocosImage->getPositionX() - 180,
cocosImage->getPositionY()));
cocosImage->runAction(move_x);
}
}
}
if (cocosImage->getPositionX() >= bg_width_min - pm * 50
&& cocosImage->getPositionX() <= bg_width_max - pm * 50) //边界判断 最小距离
{
if (abs(diff_1.x) < 30)
{
if (diff_1.x >0)
{
auto move_x = MoveTo::create(0.5, Point(cocosImage->getPositionX() + 50,
cocosImage->getPositionY()));
cocosImage->runAction(move_x);
}
else
{
auto move_x = MoveTo::create(0.5, Point(cocosImage->getPositionX() - 50,
cocosImage->getPositionY()));
cocosImage->runAction(move_x);
}
}
}
}
ScrollView 的滑动的时候 有两个效果
1.移动到规定范围后 会弹回去。但是会看到范围外的图片。2.不会有弹动效果,拖动到规定范围之后,将不能拖动。
所以 这段代码实现了 在没有出规定范围的时候 有滑动效果。
并且不会出现范围以外的图片地区。移动的时候 使用setpostion 移动结束后 增加一个动作
。起到了一个缓冲的惯性。这个横屏实例。竖屏 参照这个 修改参数即可。