cocos2d-x 3.0 游戏关卡滑动 弹动 不会出现黑边效果
发布时间:2020-12-14 18:57:07 所属栏目:百科 来源:网络整理
导读:#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: b
#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.移动到规定范围后 会弹回去。可是会看到范围外的图片。
移动的时候 使用setpostion 移动结束后 添加一个动作? 这个横屏实例。竖屏 參照这个 改动參数就可以。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |