Cocos2D Rotation and Anchor point

Jose Antony picture Jose Antony · Jun 7, 2011 · Viewed 7.3k times · Source

The problem that I have is that when ever I change the anchor point sprite automatically rotates with respect to the current anchor point. And I don't want that to happen.

The steps that I followed

  1. create a sprite with anchor point (0.5, 0.5)
  2. Changed the anchor point to (0,1)
  3. Rotated the sprite to 90 degree. (Using CCRotateBy. Sprite rotated correctly)
  4. Changed the anchor point to (0.5, 0.5) (Every thing is fine till now. And this is the position that I need to keep). Now sprite.rotation is 90.
  5. I changed the anchor point to (1,0) (Sprite automatically rotates to 90 degree with respect to the given anchor point - I need to stop this behavior)

Is there any way to reset the rotation of sprite to 0, without actually rotating the texture(ie., to keep the texture in its current form - actual texture rotated to 90 degrees) and changing anchor point or position along with step 4, so that I can continue from point 5.

Answer

Fraser Graham picture Fraser Graham · Jun 9, 2011

As Lukman says, the anchor point will always affect rotation, since your goal is to be able to specify the sprite position with a different anchor point from the rotation I would suggest making an empty CCNode as a parent of your sprite.

This way, you can set the position on sprite to be relative to this parent node to compensate for your anchor point change and then keep the anchor point for rotation on the sprite but use the parent node for position.