cocos2d-x之触摸的响应

1. BoundingBox : CCNode的一个属性,返回精灵的边界。

CCNode‘s attribute , return the side of sprite


2. getContentSize :每一个精灵都被看成是一个矩形,具有长和宽,单位是point,返回的是矩形的大小。

every sprite is regarded as a rectangle,includes length and width,unit is point,return the size of rectangle.


3. setTouchEnabled(true);
setTouchMode(kCCTouchesOneByOne);    //表示允许触摸  indicate we can touch


4. void registerWithTouchDispatcher(void);//注册触摸

bool ccTouchBegan(CCTouch* pTouch,CCEvent* pEvent); //触摸开始,注意返回类型,如果返回false,就不用写下面三个函数

void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);//触摸滑动

void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);//触摸结束
void ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent);//触摸取消,例如中途来点



开启触摸

在需要开启触摸的地方加入就行,例如init里面

  1. bool Hello::init()  
  2. {  
  3.     setTouchEnabled(true);  
  4.     return true;  
  5. }  


3、实现注册函数
  1. void Hello::registerWithTouchDispatcher()             
  2. {  
  3.     CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, true);  
  4. }  


4、实现ccTouchBegan
  1. bool Hello::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)  
  2. {     
  3.     CCPoint touchpoint = pTouch->getLocation();       //获取触摸坐标  
  4.     CCLOG("touch began, touchpoint is %f", touchpoint);  
  5.     return true;      //true表示继续响应CCTouchMove,CCTouchEnd,CCTouchCancalled,false表示不响应。  
  6. }  


5、实现ccTouchMove
  1. void Hello::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)  
  2. {     
  3.     CCPoint touchpoint = pTouch->getLocation();       //获取触摸坐标  
  4.     CCLOG("touch move, touchpoint is %f", touchpoint);  
  5. }  

6、实现ccTouchEnded
  1. void Hello::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent)  
  2. {  
  3.     CCPoint touchpoint = pTouch->getLocation(); //获取触摸坐标   
  4.     CCLOG("touch end, touchpoint is %f", touchpoint);  
  5. }  

7、实现ccTouchCancalled
  1. void Hello::ccTouchCancalled(CCTouch *pTouch, CCEvent *pEvent)  
  2. {     
  3.     CCPoint touchpoint = pTouch->getLocation();       //获取触摸坐标  
  4.     CCLOG("touch end, touchpoint is %f", touchpoint);  
  5. }  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值