cocos2d-iphonecocosbuilder

CCSprites not retaining relative position when child of another sprite


So, I'm looking to touch a specific sprite which is a child of another, which is a child of a node. The structure is

CCNode
    CCSprite | tag = 1 | node local coordinate = 0, 0
        CCSprite | tag = 2 | node local coordinate = 26.5, 133.5

I would like to detect when touch is on the second CCSprite. So, I tag them like in the example and run a method upon touch on the node like so

-(BOOL) touchIsInBoundingBox:(UITouch *) touch withEvent:(UIEvent *) event
{   //node local coordinates
    CCSprite * s = (CCSprite *)[self getChildByTag:1];
    CCSprite * t = (CCSprite *)[s getChildByTag:2];
    CGPoint local = [self convertTouchToNodeSpace:touch];
    return CGRectContainsPoint(t.boundingBox, local);
}

Now, this should work, but it turns out that t returns it's boundingBox as coordinates relative to itself, rather than modified by its position. Is there any way to get the boundingBox such that it checks the position of the sprite relative to the node, rather than relative to itself?


Solution

  • CGPoint local = [s convertTouchToNodeSpace:touch];
    

    You want to convert to the nodespace of the parent, in this case, s.