<span style="white-space:pre"> </span><span style="font-size:24px;color:#ff0000;">在1年前左右,我需要利用cocos2d-x做一个类似地图的东西。当然就需要到平移和缩放了,在很多游戏中如果能够实现缩放效果,那可以提升游戏的一个格逼!最近我又需要做类似的工作,但是我先前就删掉了那份代码,再次的上当让我觉得有必要我的发现分享给大家。</span>
<span style="font-size:24px;color:#ff0000;"><span style="white-space:pre"> </span>首先是一个手指控制平移,两个手指控制缩放(注意是按层的锚点,在我这里就是屏幕中心),旋转的话可以看情况绕锚点旋转,如果锚点不在屏幕中心那就相当别扭。所以我的这个触摸层的坐标是固定的,平移改变的其实是锚点位置,这点很重要,我觉得也很合理方便。同样我提醒一句,变旋转边平移产生的效果不是很炫酷,反而是让人头晕。我还发现写代码头晕不是因为代码,而是因为自己做的粗糙图片和垃圾特效(包括滥用粒子效果)。</span>
<span style="font-size:24px;color:#ff0000;"><span style="white-space:pre"> </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 style="white-space:pre"> </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 style="font-family: Arial,Helvetica,sans-serif;">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 style="white-space:pre"> </span>CCPoint pos_position_pre;//移动前的点坐标 <span style="white-space:pre"> </span>CCPoint pos_scale_pre[2];//缩放前的两点坐标 <span style="white-space:pre"> </span>float scale;//缩放前的值
<span style="font-size:18px;color:#ff0000;">还有一个问题是层开启了 锚点对位置的不影响。所以需要这样初始化:</span><span style="font-size: 24px; white-space: pre;"> </span>
<span style="font-family: Arial,sans-serif;"><span style="font-size:10px;">this->setTouchEnabled(true);</span></span>
<span style="font-family: Arial,sans-serif;"><span style="font-size:10px;">this->ignoreAnchorPointForPosition(false);//开启锚点对坐标的影响</span></span>
<span style="font-family: Arial,sans-serif;"><span style="font-size:10px;">this->setContentSize(CCSize(map_w,map_h));//设置层大小</span></span>
<span style="font-family: Arial,sans-serif;"><span style="font-size:10px;">this->setAnchorPoint(ccp(0.5,0.5));</span></span>
<span style="white-space: pre;"> </span><span style="font-size:18px;color:#ff0000;">一个手指平移效果,需要在began中的if(touchs_num==0)中记录下pos_position_pre。</span>
<span style="font-size:18px;color:#ff0000;"><span style="white-space: pre;"> </span>两个手指缩放效果,要在began的if(touchs_num==0)<span style="font-family: Arial,sans-serif;">和</span><span style="font-family: Arial,sans-serif; background-color: rgb(255,255);">if(t...==1)中分记录下两点的坐标。这样才能计算出两个按下手指之间的距离,再说一遍,began中一次只能接收一个点。</span></span>
<span style="font-size:18px;color:#ff0000;">然后在moved的if(touchs_num == 1 && !two_release_one)中实现平移:</span>
<span style="font-family: Arial,sans-serif;"> </span>
<span style="white-space: pre;"> </span>CCPoint pos_dt = position - pos_position_pre;//计算差值
<span style="font-family: Arial,sans-serif;">//差值除以层的大小[setContentSize设置的],再除以/scale[可以不除,效果不同</span>
this->setAnchorPoint(this->getAnchorPoint()-ccp(pos_dt.x/map_w,pos_dt.y/map_h)/scale); pos_position_pre=position;//替换前值
<span style="font-family: Arial,sans-serif;"> </span>
<span style="font-family: Arial,sans-serif;"><span style="font-size:18px;color:#ff0000;">上面的条件!two_release去掉的话会和两点操作产生冲突,会出bug,就是这样bug很烦出理,如果有兴趣可以研究,我就不再费力说了。</span></span>
<span style="font-family: Arial,sans-serif;">在moved的else if(pTouches->count() == 2 && two_release_one ==0)中出理缩放:</span>
<span style="font-family: Arial,sans-serif;">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 style="font-family: Arial,sans-serif;"><span style="font-size:18px;color:#ff0000;">它和平移不同的是在ended里面替换前值。</span></span>
if(touchs_num == 1) { <span style="white-space:pre"> </span>two_release_one = 0; } else if(touchs_num == 2) { <span style="white-space:pre"> </span>two_release_one = 1; <span style="white-space:pre"> </span>scale = this->getScale(); }
<span style="font-family: Arial,sans-serif;"><span style="font-size:18px;color:#ff0000;">大概就如此了,最后给出完整的源文件,可以结合头文件研究研究,总体按照这个方式可以正确的平移和缩放,如果想要实现旋转就更方便了,应为它的锚点就在屏幕中心。</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 instance bool 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--; } }