Search code examples
androidcocos2d-iphonerotationscale

Rotating & Scaling a Sprite image in COCOS2D Android


I am using COCOS2D for Android. I want to rotate a rectangular sprite and scale it to different heights, but two of its vertices being at a fixed place. I mean, out of four vertices of a rectangle, I want only two vertices to move to different points and even scale the rectangle to varying length. Similar example for varying length: Thermometer,where rectangular mercury level goes up & down fixing two vertices at the bottom.


Solution

  • To accomplish this, move your anchorPoint to the edge that you wish to remain constant. Lets say you move your anchorPoint to the bottom edge of your thermometer sprite, when you scale it, only the top edge will move. The anchorPoint has a float value of 0..1 corresponding to 0-100% of the sprites dimensions.

    Please note, this will affect your position to you may need to adjust by 1/2 of the sprites width (or height, depending on how you set it up). The anchorPoint of your sprite resides where your sprite's position is. So by default, the anchorPoint is located at (0.5, 0.5) which is why your sprite is centered wherever you position it. If you were to alter the anchorPoint to (0.5, 0) then the sprite would be centered horizontally, but the bottom of the sprite would align with wherever you set its position to be, and it would scale away from the anchor point.

    These links will also prove to be invaluable to you:

    http://www.qcmat.com/understanding-anchorpoint-in-cocos2d/

    http://cocos2d-central.com/topic/436-position-vs-anchorpoint/

    http://www.cocos2d-iphone.org/forum/topic/1557