cocos2d-x之触摸的响应

来源:互联网 发布:有线网络打印机变无线 编辑:程序博客网 时间:2024/06/10 00:04

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
原创粉丝点击