Cocos2d-x 中的多点触控陷阱,和实现移动和缩放。

Cocos2d-x 中的多点触控陷阱,和实现移动和缩放。,第1张

概述<span style="white-space:pre"> </span><span style="font-size:24px;color:#ff0000;">在1年前左右,我需要利用cocos2d-x做一个类似地图的东西。当然就需要到平移和缩放了,在很多游戏中如果能够实现缩放效果,那可以提升游戏的一个格逼!最近我又需要做类似的工作,但是我先前就删掉了那份代码,再次的上当让我觉得有必要我的发现

<span >	</span><span >在1年前左右,我需要利用cocos2d-x做一个类似地图的东西。当然就需要到平移和缩放了,在很多游戏中如果能够实现缩放效果,那可以提升游戏的一个格逼!最近我又需要做类似的工作,但是我先前就删掉了那份代码,再次的上当让我觉得有必要我的发现分享给大家。</span>
<span ><span >	</span>首先是一个手指控制平移,两个手指控制缩放(注意是按层的锚点,在我这里就是屏幕中心),旋转的话可以看情况绕锚点旋转,如果锚点不在屏幕中心那就相当别扭。所以我的这个触摸层的坐标是固定的,平移改变的其实是锚点位置,这点很重要,我觉得也很合理方便。同样我提醒一句,变旋转边平移产生的效果不是很炫酷,反而是让人头晕。我还发现写代码头晕不是因为代码,而是因为自己做的粗糙图片和垃圾特效(包括滥用粒子效果)。</span>
<span ><span >	</span>好,回到正题。首先MapLayer.h是这样的,就是触摸层。</span>
#ifndef __MAP_LAYER_H__#define __MAP_LAYER_H__#include "cocos2d.h"USING_NS_CC;class Map : public cocos2d::cclayer{public:	float map_x,map_y,map_w,map_h;	CCPoint pos_position_pre;	CCPoint pos_scale_pre[2];	int  touchs_num; 	bool two_release_one;	float scale;    virtual bool init();  	voID cctouchesBegan(CCSet *ptouches,CCEvent *pEvent);	voID cctouchesMoved(CCSet *ptouches,CCEvent *pEvent);    voID cctouchesEnded(CCSet *ptouches,CCEvent *pEvent);	voID update(float dt);	voID draw();    // implement the "static node()" method manually    CREATE_FUNC(Map);};
首先,我们需要touchs_num记录实际当前按着的手指数量。因为ptouchs->count()返回的根本不是人们所想得值。Began里的值是有新手指按下就会产生1,而且它只会返回1,即便是两个手指同时按下,实际上两个手指不可能同时按下,就好比比划一个V字再慢慢的插自己的眼睛总有一个眼睛先有感觉。而程序60帧每秒,那速度就更快了。所以在began里面写:
voID Map::cctouchesBegan(CCSet *ptouches,CCEvent *pEvent){	if(ptouches->count() == 1)	{		if(touchs_num == 0  )		{			CCtouch* touch = dynamic_cast<CCtouch*> (ptouches->anyObject());			CCPoint position = touch->getLocationInVIEw();			position = CCDirector::sharedDirector()->convertToGL(position);<span >			</span>//这里处理一个按键 *** 作的相关初始化		}		else if(touchs_num == 1  )		{			CCtouch* touch = dynamic_cast<CCtouch*> (ptouches->anyObject());			CCPoint position = touch->getLocationInVIEw();			position = CCDirector::sharedDirector()->convertToGL(position);			//这里处理两个按键 *** 作的相关初始化

}

touchs_num++;

}

}

 每传入一次事件,touchs_num就加1,不用多说。相应的在Ended里需要减1,因为它和began一样,每次都只能穿一个坐标进来。我想说的是这种设计真的很坑爹。反正我在安卓上测试至少是这种效果。 
 
<span >voID Map::cctouchesEnded(CCSet *ptouches,CCEvent *pEvent)</span>
{	if(ptouches->count() == 1 )	{		CCtouch* touch = dynamic_cast<CCtouch*> (ptouches->anyObject());		CCPoint position = touch->getLocationInVIEw();		position = CCDirector::sharedDirector()->convertToGL(position);		if(touchs_num == 1)		{			two_release_one = 0;					}		else if(touchs_num == 2)		{			two_release_one = 1;			scale = this->getScale();		}			cclog("touchend\n");		touchs_num--;	}}

仔细看上面,其中有一个布尔变量叫two_release_one,意思是否两个手指移开了一个手指。因为仅靠touhs_num无法分辨是点击了一个手指还是两个手指移开了一个的情况。核心的处理就在Moved,我推荐利用两次调用的变量差值实现动态效果,所以类还需要几个变量存储上一次的属性值,比如
 
<span >	</span>CCPoint pos_position_pre;//移动前的点坐标<span >	</span>CCPoint pos_scale_pre[2];//缩放前的两点坐标<span >	</span>float scale;//缩放前的值
<span >还有一个问题是层开启了 锚点对位置的不影响。所以需要这样初始化:</span><span >	</span>
<span ><span >this->settouchEnabled(true);</span></span>
<span ><span >this->ignoreAnchorPointForposition(false);//开启锚点对坐标的影响</span></span>
<span ><span >this->setContentSize(CCSize(map_w,map_h));//设置层大小</span></span>
<span ><span >this->setAnchorPoint(ccp(0.5,0.5));</span></span>
<span >	</span><span >一个手指平移效果,需要在began中的if(touchs_num==0)中记录下pos_position_pre。</span>
<span ><span >	</span>两个手指缩放效果,要在began的if(touchs_num==0)<span >和</span><span >if(t...==1)中分记录下两点的坐标。这样才能计算出两个按下手指之间的距离,再说一遍,began中一次只能接收一个点。</span></span>
 
<span >然后在moved的if(touchs_num == 1 && !two_release_one)中实现平移:</span>
<span ></span>
<span >	</span>CCPoint pos_dt = position - pos_position_pre;//计算差值
<span >//差值除以层的大小[setContentSize设置的],再除以/scale[可以不除,效果不同</span>
this->setAnchorPoint(this->getAnchorPoint()-ccp(pos_dt.x/map_w,pos_dt.y/map_h)/scale);pos_position_pre=position;//替换前值
<span ></span>
<span ><span >上面的条件!two_release去掉的话会和两点 *** 作产生冲突,会出BUG,就是这样BUG很烦出理,如果有兴趣可以研究,我就不再费力说了。</span></span>
<span >在moved的else if(ptouches->count() == 2 && two_release_one ==0)中出理缩放:</span>
<span >CCPoint temp_pre=pos_scale_pre[0]-pos_scale_pre[1];//向量,为了后面求两点距离</span>
CCPoint temp_Now=positions[0]-positions[1];this->setScale(scale*temp_Now.getLength()/temp_pre.getLength());
 
<span ><span >它和平移不同的是在ended里面替换前值。</span></span>
if(touchs_num == 1){<span >	</span>two_release_one = 0;}else if(touchs_num == 2){<span >	</span>two_release_one = 1;<span >	</span>scale = this->getScale();}
<span ><span >大概就如此了,最后给出完整的源文件,可以结合头文件研究研究,总体按照这个方式可以正确的平移和缩放,如果想要实现旋转就更方便了,应为它的锚点就在屏幕中心。</span></span>
#include "MapLayer.h"USING_NS_CC;const int MAP_START_W	=	512;const int MAP_START_H	=	512;const int MAP_START_X	=	1280/2;const int MAP_START_Y	=	210+512/2;// on "init" you need to initialize your instancebool Map::init(){ 	scale=1.0; 	two_release_one=0;	touchs_num =0;	map_x = MAP_START_X;	map_y = MAP_START_Y;	map_h = MAP_START_H;	map_w = MAP_START_W;    //////////////////////////////    // 1. super init first    if ( !cclayer::init() )    {        return false;    }    	this->scheduleUpdate();//开启update函数	this->settouchEnabled(true);	this->ignoreAnchorPointForposition(false);//开启锚点    CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();    CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); //Origin 起源	this->setContentSize(CCSize(map_w,map_h));	this->setAnchorPoint(ccp(0.5,0.5));	this->setposition(ccp(map_x,map_y));    	CCSprite* spr_tower_arrow = CCSprite::create("resource/image/tower_arrow.png");	spr_tower_arrow->setposition(ccp(0,0));	this->addChild(spr_tower_arrow,1);    return true;}voID Map::draw(){	gllinewidth(2.0);	ccDrawcolor4B(255,255);			ccDrawline(ccp(0,0),ccp(0,MAP_START_H));		ccDrawline(ccp(0,ccp(MAP_START_W,0));		ccDrawline(ccp(0,MAP_START_H),MAP_START_W));		ccDrawline(ccp(MAP_START_W,MAP_START_W));		}voID Map::update(float dt){		cclog("%f,%f,%f",this->getScale(),this->getAnchorPoint().x,this->getAnchorPoint().y);}voID Map::cctouchesBegan(CCSet *ptouches,CCEvent *pEvent){	if(ptouches->count() == 1)	{		if(touchs_num == 0  )		{			CCtouch* touch = dynamic_cast<CCtouch*> (ptouches->anyObject());			CCPoint position = touch->getLocationInVIEw();			position = CCDirector::sharedDirector()->convertToGL(position);			pos_position_pre=position;			//////////////////////////////////////////////////////////////////////////			pos_scale_pre[0] = position;		}		else if(touchs_num == 1  )		{			CCtouch* touch = dynamic_cast<CCtouch*> (ptouches->anyObject());			CCPoint position = touch->getLocationInVIEw();			position = CCDirector::sharedDirector()->convertToGL(position);			pos_scale_pre[1] = position;		}				touchs_num++;	}	}voID Map::cctouchesMoved(CCSet *ptouches,CCEvent *pEvent){	if(ptouches->count() == 1)	{		if(touchs_num == 1  && !two_release_one)		{			CCtouch* touch = dynamic_cast<CCtouch*> (ptouches->anyObject());			CCPoint position = touch->getLocationInVIEw();			position = CCDirector::sharedDirector()->convertToGL(position);			CCPoint pos_dt = position - pos_position_pre;						this->setAnchorPoint(this->getAnchorPoint()-ccp(pos_dt.x/map_w,pos_dt.y/map_h)/scale);			pos_position_pre=position;		}								cclog("touchmove\n");	}	else if(ptouches->count() == 2 && two_release_one ==0)	{		CCPoint positions[2];		int i=0;		for(CCSetIterator it = ptouches->begin() ; it != ptouches->end();it++,i++)		{			CCtouch* touch = dynamic_cast<CCtouch*>(*it);			CCPoint position = touch->getLocationInVIEw();			positions[i] = CCDirector::sharedDirector()->convertToGL(position);		}		CCPoint temp_pre=pos_scale_pre[0]-pos_scale_pre[1];		CCPoint temp_Now=positions[0]-positions[1];			this->setScale(scale*temp_Now.getLength()/temp_pre.getLength());	}}voID Map::cctouchesEnded(CCSet *ptouches,CCEvent *pEvent){	if(ptouches->count() == 1 )	{		CCtouch* touch = dynamic_cast<CCtouch*> (ptouches->anyObject());		CCPoint position = touch->getLocationInVIEw();		position = CCDirector::sharedDirector()->convertToGL(position);		if(touchs_num == 1)		{			two_release_one = 0;					}		else if(touchs_num == 2)		{			two_release_one = 1;			scale = this->getScale();		}			cclog("touchend\n");		touchs_num--;	}}
总结

以上是内存溢出为你收集整理的Cocos2d-x 中的多点触控陷阱,和实现移动和缩放。全部内容,希望文章能够帮你解决Cocos2d-x 中的多点触控陷阱,和实现移动和缩放。所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1055511.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存