ioscocos2d-iphonecgpointccnode

Get position of CCSprite


Im a bit confused. I am trying to move one CCSprite onto the position of another CCSprite. I figured it would be simple, but the CCSprite isn't positioning correctly. I have 2 CCSprites:

//Get Position of EMPTY SPRITE
CCSprite *sprite = (CCSprite*)[self getChildByName:[NSString stringWithFormat:@"%d", kEMPTY] recursively:NO];

 //GET DRAGGED SQUARE
 CCSprite *spriteMove = (CCSprite*)[self getChildByName:[NSString stringWithFormat:@"%d", DragStartSquare] recursively:NO];

I am then trying to get the position of the EMPTY SPRITE (Its not actually empty) by doing

CGPoint worldCoord = [sprite convertToWorldSpace: sprite.position];

I am then animating it with:

id action1 = [CCActionMoveTo actionWithDuration:duration position:worldCoord];

This sends the sprite off the screen. What am I doing wrong?


Solution

  • You need to convert back into the target sprite's node space:

    CGPoint targetNodeCoord = [spriteMove convertToNodeSpace: [sprite convertToWorldSpace: sprite.position]];
    CCAction *action1 = [CCActionMoveTo actionWithDuration:duration position:targetNodeCoord];