Note that there are some explanatory texts on larger screens.

plurals
  1. POCocos2d-x and handling touch events
    primarykey
    data
    text
    <p>I have my sprites on screen and I have a vector that stores each sprite.</p> <p>Can a CCSprite* handle a touch event? Or just the CCLayer*?</p> <p>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:</p> <p><code>class Field : public cocos2d::CCSprite, public cocos2d::CCTargetedTouchDelegate</code></p> <p>and I implement functions:</p> <pre><code> 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(); </code></pre> <p>I put CCLOG statements in each one and I dont hit them!</p> <p>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.</p> <p>UPDATE: The code I have been trying:</p> <pre><code>Field* Field::createWithLocation(cocos2d::CCPoint p) { Field* f = new Field(); f-&gt;autorelease(); f-&gt;initWithLocation(p); return f; } void Field::initWithLocation(cocos2d::CCPoint p) { setFieldCenterPoint(p); setFieldGraphicName(FIELD::fieldIconFileName); setFieldSprite(cocos2d::CCSprite::create(getFieldGraphicName().c_str())); getFieldSprite()-&gt;setPosition(ccp(getFieldCenterPoint().x, getFieldCenterPoint().y)); setFieldSize(getFieldSprite()-&gt;getContentSize()); } cocos2d::CCRect Field::rect() { cocos2d::CCSize s = getFieldSprite()-&gt;getTexture()-&gt;getContentSize(); return cocos2d::CCRectMake(-s.width / 2, -s.height / 2, s.width, s.height); } void Field::onEnter() { CCLOG("In onEnter"); cocos2d::CCDirector* pDirector = cocos2d::CCDirector::sharedDirector(); pDirector-&gt;getTouchDispatcher()-&gt;addTargetedDelegate(this, 0, true); //_dir-&gt;Instance()-&gt;getDirector()-&gt;getTouchDispatcher()-&gt;addTargetedDelegate(this, 0, true); cocos2d::CCSprite::onEnter(); } void Field::onExit() { CCLOG("In onExit"); cocos2d::CCDirector* pDirector = cocos2d::CCDirector::sharedDirector(); pDirector-&gt;getTouchDispatcher()-&gt;removeDelegate(this); //_dir-&gt;Instance()-&gt;getDirector()-&gt;getTouchDispatcher()-&gt;removeDelegate(this); cocos2d::CCSprite::onExit(); } bool Field::containsTouchLocation(cocos2d::CCTouch* touch) { return rect().containsPoint(convertTouchToNodeSpaceAR(touch)); } bool Field::ccTouchBegan(cocos2d::CCTouch* touch, cocos2d::CCEvent* event) { CCLOG("In ccTouchBegan"); return true; } void Field::ccTouchMoved(cocos2d::CCTouch* touch, cocos2d::CCEvent* event) { CCLOG("In ccTouchMoved"); } void Field::ccTouchEnded(cocos2d::CCTouch* touch, cocos2d::CCEvent* event) { CCLOG("In ccTouchEnded"); } void Field::touchDelegateRetain() { this-&gt;retain(); } void Field::touchDelegateRelease() { this-&gt;release(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload