您的位置:首页 > 移动开发 > Cocos引擎

cocos2d-x之触摸的响应

2014-07-03 20:25 127 查看
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里面

bool Hello::init()  

{  

    setTouchEnabled(true);  

    return true;  

}  

3、实现注册函数

void Hello::registerWithTouchDispatcher()             

{  

    CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, true);  

}  

4、实现ccTouchBegan

bool Hello::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)  

{     

    CCPoint touchpoint = pTouch->getLocation();       //获取触摸坐标  

    CCLOG("touch began, touchpoint is %f", touchpoint);  

    return true;      //true表示继续响应CCTouchMove,CCTouchEnd,CCTouchCancalled,false表示不响应。  

}  

5、实现ccTouchMove

void Hello::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)  

{     

    CCPoint touchpoint = pTouch->getLocation();       //获取触摸坐标  

    CCLOG("touch move, touchpoint is %f", touchpoint);  

}  

6、实现ccTouchEnded

void Hello::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent)  

{  

    CCPoint touchpoint = pTouch->getLocation(); //获取触摸坐标   

    CCLOG("touch end, touchpoint is %f", touchpoint);  

}  

7、实现ccTouchCancalled

void Hello::ccTouchCancalled(CCTouch *pTouch, CCEvent *pEvent)  

{     

    CCPoint touchpoint = pTouch->getLocation();       //获取触摸坐标  

    CCLOG("touch end, touchpoint is %f", touchpoint);  

}  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: