0

I'm really struggling to understand cocos2d 2.0 and how its layers and coordinates work. I have a single scene with a single layer that contains a single sprite. Here are the stats:

Layer: Position: (0,0) Origin: (-384, -430) Size: (768, 860) (same as the view) AnchorPoint: (0.5, 0.5) ignoreAnchorPointForPosition: NO Scale: 1.0

Sprite: Position: (768, 860) Origin: (499.5, 413.5) Size: (537, 893) AnchorPoint: (0.5, 0.5)

I add the sprite in the init of the layer.

How is it that the sprite is positioned at the center of the view in the iPad simulator? If someone could help me draw a mental picture (or an actual one :)) of this, that would really help.

This image shows how I understand the placement and it doesn't make any sense. If this is a bad question or not the correct forum, please let me know and I'll move it to the correct place.enter image description here

Matt Becker
  • 2,338
  • 1
  • 28
  • 36

1 Answers1

1

Anchor point is relative point on the node. (0.f, 0.f) corresponds to left-bottom corner and (1.f, 1.f) is for right-top corner. The position, that you set to the node, is set for anchor point. Of course, if it's property isRelatieveAnchorPoint is YES.

So, if anchor point is (0.5f, 0.5f), you set the position of center of the node. Thats why your layer is placed in (0.f, 0.f) of world coordinates with it's center.

But inner coordinates are always count from (0.f, 0.f) of current node. So, if you add your sprite to (768.f, 860.f) with sprite's anchor point (0.5, 0.5f), sprite's center will be positioned to this coordinates relatieve to the parent layer's origin.

I hope i described it clear enough =)

Morion
  • 10,495
  • 1
  • 24
  • 33
  • In other words: node positions are (normally) relative to their parent's position. – CodeSmile Jun 23 '12 at 10:43
  • Yeah, I wasn't thinking of the scene as being the base node. So in my case, the layer was positioned at the bottom left corner and centered on that position. So when I added the sprite at the top left of the layer and centered the sprite on it's anchor point, it actually centered it on the screen. I wrote a post about it since it was a little confusing. [http://beckerwebsite.com/?p=164](http://beckerwebsite.com/?p=164) – Matt Becker Jun 23 '12 at 16:55