Can any one explain difference between position and anchor point in cocos-2D with some example.I searched in google but cannot find good explanation,thanks in advance .
-
Read my explanation here: http://stackoverflow.com/a/7810180/201863 – CodeSmile Jul 05 '12 at 19:57
2 Answers
Suppose you have a square which is 10x10. If you say that you want to position it on your screen at position (50,40) then you need to know where that position refers to - the top left of your square, bottom left, etc.
The anchor point refers to this position. So, if your anchor point is (0,0) then the position (50,40) will be the position of the top left corner of your square.
If your anchor point is (10,0) then the position (50,40) will be the position of the top right corner of your square and so the top-left corner will be at (40,40).
So, the anchor point is the point that is positioned, and is then relative to your square.
Another example - suppose you have a building 100 floors high. Now, suppose you are a giant and you are 4 floors tall. If you are told to put your feet (that's your anchor point) on the 3rd floor, then your head will be on the 7th floor. If you were told to put your head (that's now your anchor point) on the 7th floor, then your feet would be on the 3rd. You are still in the same place, but your reference point (the anchor) has been changed.

- 4,276
- 1
- 18
- 25
-
5Correction: anchorPoint 0,0 is the lower left corner, not top left corner. Cocos2D uses a different coordinate system than UIKit. – CodeSmile Jul 05 '12 at 19:58
-
Also anchor point values range from 0 to 1, so anchor point `(10, 0)` makes no sense. – trojanfoe Aug 26 '13 at 14:04
-
The question was "explain the difference between position and anchor point", not "how exactly do I use anchor point". If you are going to be that picky, why not point out that giants don't exist and so it wouldn't be possible to stand on the 3rd floor with your head on the 7th floor? – Nick Bull Sep 04 '13 at 12:00
The position property is a CGPoint that specifies the position of the layer relative to its superlayer, and is expressed in the superlayer's coordinate system.
The anchorPoint property is a CGPoint that specifies a location within the bounds of a layer that corresponds with the position coordinate. The anchor point specifies how the bounds are positioned relative to the position property, as well as serving as the point that transforms are applied around. It is expressed in the unit coordinate system-the (0.0,0.0) value is located closest to the layer’s origin and (1.0,1.0) is located in the opposite corner. Applying a transform to the layer’s parent (if one exists) can alter the anchorPoint orientation, depending on the parent’s coordinate system on the y-axis and also see this link

- 6,702
- 7
- 42
- 65