Cocos2dx 2.0x Touch事件

2.0版

CCSprite

继承Standard Touch Delegate 或 Targeted Touch Delegate

并实现相应的函数

    virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
    virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);
    virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);
    virtual void ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent);

    virtual void ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent);
    virtual void ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent);
    virtual void ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent);
    virtual void ccTouchesCancelled(CCSet *pTouches, CCEvent *pEvent);

注册  删除消息响应

onEnter()
{
    //...
    CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, true); 
}

onExit()
{
    //...
    CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);
}



CCLayer

onEnter()
{
    //...
    setTouchEnabled(true);
}

默认注册为多点触控,单点触控需要重写

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



你可能感兴趣的:(Cocos2dx 2.0x Touch事件)