Sunday, August 5, 2012

Cocos2d Anchor Point

The anchor point for a CCNode is a little confusing at first.  The anchor points are relative to the size of the node.  The most important thing to remember is what the position represents.  in CCNode.h it says:

/** Position (x,y) of the node in points. (0,0) is the left-bottom corner. */

The default anchor point for a CCNode is (0.5, 0.5).  Look at your node.  Start from the bottom left corner of your node and go 0.5*nodeWidth to the right, 0.5*nodeHeight up and that's the node's anchor point.

Here are some examples:
An anchor point of (0.5,0.5) at position (0,0), the node's center will be at position (0,0)
An anchor point of (0,0) at position (0,0), the node's bottom left corner will be at position (0,0)
An anchor point of (1,1) at position (0,0), the node's top right corner will be at position (0,0)
An anchor point of (-1,-1) at position (0,0), the node's bottom left corner will be at position (nodeWidth,nodeHeight)

No comments:

Post a Comment