zoukankan      html  css  js  c++  java
  • cocos2d-x 3.0 游戏关卡滑动 弹动 不会出现黑边效果

    #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 移动结束后 添加一个动作 
    。起到了一个缓冲的惯性。这个横屏实例。竖屏 參照这个 改动參数就可以。

  • 相关阅读:
    CentOS-6.8 最详细安装教程(贴镜像文件+多图)
    SSM 框架整合完整流程讲解(IDEA + Maven)
    【万字长文】Spring MVC 层层递进轻松入门 !
    Spring AOP 由浅入深学习教程【新手向】
    Spring 注解和XML两种方式配置IOC
    【万字长文】Spring框架 层层递进轻松入门 (IOC和DI)
    用idea搭建SSM项目,原来这么简单
    Java并发编程:什么是CAS?这回总算知道了
    Java并发编程:Java的四种线程池的使用,以及自定义线程工厂
    Java并发编程:Java线程池核心ThreadPoolExecutor的使用和原理分析
  • 原文地址:https://www.cnblogs.com/zhchoutai/p/8376982.html
Copyright © 2011-2022 走看看