Cocos 2d-x Finding coordinates of child of sprite when layer has moved
with CCFollow
I haven't been able to figure this out and it's driving me crazy.
I have a CCSprite that is followed by the camera with CCFollow. Calling
get position on the sprite gives me the expected coordinate, the
coordinate of the layer, such that if I click on any part of the layer,
the sprite moves to this position. The sprite itself has CCSprite children
(turrets), anchored at different positions, that should track a particular
position or enemy when clicked.
Like so
CCPoint turretPos =
m_turret->getParent()->convertToWorldSpace(m_turret->getPosition());
CCPoint targetPos = m_target->position();
CCPoint trackVector = ccpSub(turretPos, targetPos);
But now that the Layer moves, this does not work as the screen coordinates
and the layer coordinates do not line up. Without CCFollow, this works
perfectly.
If I call the sprite's parent sprite:
CCPoint pos = m_turret->getParent()->getPosition();
It gives me the expected value. But I can't get the child of that sprite
in that coordinate frame. I tried
m_turret->getParent()->getParent()->convertToNodeSpace but that fails as
well. What am I missing?
EDIT:
Figured an (annoying) way to do this. First convert the child's
coordinates to world
(m_turret->convertToWorldSpaceAR(m_turret->getPosition()), then, on the
parent's parent (the layer, in this case, not the parent sprite) call the
convertToNodeSpace method to place them in the proper coordinate frame. I
don't know of any easier method to do this so anyone with a better method,
please, contribute!
No comments:
Post a Comment