打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
【玩转cocos2d-x之十八】仿《中国好学霸》文字拖拽和定位

原创作品,转载请标明http://blog.csdn.net/jackystudio/article/details/13287519


现在各种猜成语猜歌名好学霸之类的游戏火的一塌糊涂。本节就介绍下文字的拖拽和定位。


1.基本原理

其实这只是精灵的简单拖拽和坐标的识别而已。当触摸点在精灵的范围内,精灵可以感应拖动,当触摸结束进行位置判断,如果在有效范围内就进行自动定位。


2.实现

2.1.背景加入和文字精灵的加入

这里是采用这一节http://blog.csdn.net/jackystudio/article/details/13014883所述方式添加中文。

  1. bool AutoSet::init()  
  2. {  
  3.     bool bRet=false;  
  4.     do   
  5.     {  
  6.         CC_BREAK_IF(!CCLayer::init());  
  7.         CCSize visiableSize=CCDirector::sharedDirector()->getVisibleSize();  
  8.   
  9.         CCSprite* background=CCSprite::create("AutoSetBk.jpg");  
  10.         background->setPosition(ccp(visiableSize.width/2,visiableSize.height/2));  
  11.         this->addChild(background);//添加背景  
  12.   
  13.         //利用CCDictionary来读取xml  
  14.         CCDictionary* chnStrings = CCDictionary::createWithContentsOfFile("CHN_Strings.xml");  
  15.         const char *hao = ((CCString*)chnStrings->objectForKey("hao"))->m_sString.c_str();   
  16.   
  17.         text=CCLabelTTF::create(hao,"Arial",50);  
  18.         text->setPosition(ccp(120,160));  
  19.         text->setColor(ccc3(0,0,0));  
  20.         this->addChild(text);//添加文本  
  21.   
  22.         this->setTouchEnabled(true);//设置触摸可用  
  23.   
  24.         bRet=true;  
  25.     } while (0);  
  26.     return bRet;  
  27. }  


2.2.触摸的实现和拖拽的定位

因为3.0版本cocos2d-x的触摸实现已经变更了,所以这里不再赘述,3.0之前的触摸的原理和实现具体可以参见这一节http://blog.csdn.net/jackystudio/article/details/11860007

2.2.1注册触摸事件

  1. void AutoSet::registerWithTouchDispatcher(void)  
  2. {  
  3.     CCDirector *pDirector=CCDirector::sharedDirector();    
  4.     pDirector->getTouchDispatcher()->addTargetedDelegate(this,0,true);//单点触摸  
  5. }  

2.2.2.触摸开始

  1. bool AutoSet::ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)  
  2. {  
  3.     return true;//返回true表示接收触摸事件  
  4. }  

2.2.3.触摸过程

  1. void AutoSet::ccTouchMoved(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)  
  2. {  
  3.     CCPoint beginPoint = pTouch->getLocationInView();  //获取触摸位置  
  4.     beginPoint = CCDirector::sharedDirector()->convertToGL(beginPoint);//坐标转换  
  5.     CCPoint pt=text->getPosition();  
  6.     CCRect rect=CCRectMake(pt.x-30,pt.y-30,60,60);  
  7.     if (rect.containsPoint(beginPoint))//判断触摸点是否在文字上  
  8.     {  
  9.         CCPoint endPoint=pTouch->getPreviousLocationInView();//获取触摸的前一个位置    
  10.         endPoint=CCDirector::sharedDirector()->convertToGL(endPoint);     
  11.   
  12.         CCPoint offSet =ccpSub(beginPoint,endPoint);//获取offset    
  13.         CCPoint toPoint=ccpAdd(text->getPosition(),offSet); //获取真正移动位置  
  14.         text->setPosition(toPoint);//移动文字  
  15.     }  
  16. }  

2.2.4.触摸结束

  1. void AutoSet::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent)  
  2. {  
  3.     CCPoint lastPoint = pTouch->getLocationInView();//获取触摸结束点位置  
  4.     lastPoint = CCDirector::sharedDirector()->convertToGL(lastPoint);  
  5.     CCRect rect=CCRectMake(330,130,60,60);  
  6.     CCMoveTo* moveto;  
  7.     if (!rect.containsPoint(lastPoint))//如果未在指定区域,还原到初始位置  
  8.     {  
  9.         moveto=CCMoveTo::create(0.1f,ccp(120,160));  
  10.     }  
  11.     else//如果在指定区域,移动到该区域中心  
  12.     {  
  13.         moveto=CCMoveTo::create(0.1f,ccp(360,160));  
  14.     }  
  15.     text->runAction(moveto);  
  16. }  

3.效果图


4.源码下载

http://download.csdn.net/detail/jackyvincefu/6463261

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
[cocos2d
Innovus教程 - Timing报告详细解读
Unity3D:Gizmos画圆(原创)
C#软件开发实例.私人订制自己的屏幕截图工具(四)基本截图功能实现
uCGUI使用
8.3.1 三个绘图工具类详解
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服