cocos2d-x and handling touch events

Posted by Jason on Game Development See other posts from Game Development or by Jason
Published on 2013-06-27T18:10:02Z Indexed on 2013/06/27 22:29 UTC
Read the original article Hit count: 660

I have my sprites on screen and I have a vector that stores each sprite.

Can a CCSprite* handle a touch event? Or just the CCLayer*?

What is the best way to decide what sprite was touched? Should I store the coordinates of where the sprite is (in the sprite class) and when I get the event, see if where the user touched is where the sprite is by looking through the vector and getting each sprites current coordinates?

UPDATE: I subclass CCSprite:

class Field : public cocos2d::CCSprite, public cocos2d::CCTargetedTouchDelegate

and I implement functions:

    cocos2d::CCRect rect();

    virtual void onEnter();
    virtual void onExit();

    bool containsTouchLocation(cocos2d::CCTouch* touch);
    virtual bool ccTouchBegan(cocos2d::CCTouch* touch, cocos2d::CCEvent* event);
    virtual void ccTouchMoved(cocos2d::CCTouch* touch, cocos2d::CCEvent* event);
    virtual void ccTouchEnded(cocos2d::CCTouch* touch, cocos2d::CCEvent* event);

    virtual void touchDelegateRetain();
    virtual void touchDelegateRelease();

I put CCLOG statements in each one and I dont hit them!

When I touch the CCLayer this sprite is on though I do hit those in the class that implements the Layer and puts these sprites on the layer.

© Game Development or respective owner

Related posts about c++

Related posts about cocos2d-iphone