0
votes

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.

1
Your last edit explains how, not why. Why, is for the reasons I mentioned in my answer.prototypical

1 Answers

1
votes

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.