0

I'm using the basic Xcode template project. In the HelloWorldLayer I'm adding a square CCSprite as a child node with position CGPointZero. I'm also adding a CCLayerColor subclass with position CGPointZero as a child node. When drawn the sprite has its center point aligned with the lower-left corner of the screen. However the layer has its lower-left corner aligned with the screen's lower-left corner.

Why are the sprite and layer aligned differently? I'd expect both to have their center point aligned with the screen's lower-left corner as they both have their positions set to (0,0).

iPhone simulator display

Edit - more info: sorry if the screenshot is hard to decipher. The sprite is using icon-small-50.png. The layer has its content size set to (100,100). Note I tried setting the position of the layer to (10,10) to make sure its lower-left corner is actually aligned with the screen's lower-left corner (when position is (0,0)).

Edit - more info: just noticed this in CCNode.h:

// If YES, the Anchor Point will be (0,0) when you position the CCNode.
// Used by CCLayer and CCScene
BOOL ignoreAnchorPointForPosition_;

I think that explains it.

SundayMonday
  • 19,147
  • 29
  • 100
  • 154

1 Answers1

1

A CCSprite has a default anchorPoint of (0.5,0.5), which is the center of the sprite. It's done this way because sprites tend to be smaller objects that you move around as opposed to a layer which is usually a screen.

Having the registration point in the center is advantageous when rotating a sprite, as you typically want to rotate based on the center of the sprite.

It's not typical for you to rotate a CCLayer as it's typically not something you move around, but something you usually add your sprites to, such as a background image.

prototypical
  • 6,731
  • 3
  • 24
  • 34
  • 2
    Also collision detection is a factor. Especially when it comes to determining the distance between two objects you want to use their center position, not the lower left corner. To align the sprite simply add half the sprite's contentSize width & height to the sprite's position. – CodeSmile Jan 10 '13 at 12:38