Cocos2d/Cocos2d-x Attaching an arrow (sprite) to another body sprite (person)

Posted by Satchmo Brown on Game Development See other posts from Game Development or by Satchmo Brown
Published on 2014-06-25T02:26:18Z Indexed on 2014/08/24 10:32 UTC
Read the original article Hit count: 289

Filed under:
|
|

I am trying to set up a simple bow and arrow game. When the arrow hits the enemy body, the arrow's body is deleted and the arrow sprite continues to update, keeping the position correct in relation to the enemy it hit. Picture an arrow sticking into a body and that body still rotating and moving. My problem is that the rotation is completely wrong when the enemy rotates. I know how to do this in 3d with matrix transformation but I can't seem to figure it out in 2d with Cocos.

Here is my method.

  1. I save offset at which the arrow hit the enemy.
  2. Every frame, I make the rotation of the sprite match the rotation of the enemy.
  3. Then, I apply the offset I took initially which is where the arrow hit the enemy.

When they rotate, they rotate about their respective anchors and I am wondering if I need to set the anchor of the arrow to the center of the sprite. Does anyone know of an easy way to do this. If not, I will try to create an algorithm where the anchor is set to the offset divided by the width and height of the sprite image hopefully giving me the correct anchor values.

Then I assume I need to reposition the sprite.

Does anyone have a simpler way to do this?

© Game Development or respective owner

Related posts about sprites

Related posts about cocos2d