Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
manual:pivot_points [2013/03/05 10:19]
127.0.0.1 external edit
manual:pivot_points [2013/05/25 16:13] (current)
daniel
Line 1: Line 1:
 +====== Pivot Points ======
 +
 +Since Sparrow 1.2, display objects contain two additional properties: ''​pivotX''​ and ''​pivotY''​. The pivot point of an object (also know as "​origin",​ "​root"​ or "​anchor"​) defines the root of its coordinate system. ​
 +
 +Per default, the pivot point is at ''​(0,​ 0)'';​ in an image, that is the top left position. Most of the time, this is what you want. Sometimes, however, you want to have that root at a different position --- e.g. when you want to rotate an image around its center. Until now, you had to move the object to a container sprite to be able to do that:
 +
 +<code objc>
 +SPImage *image = [SPImage imageWithContentsOfFile:​@"​texture.png"​];​
 +
 +SPSprite *sprite = [SPSprite sprite];
 +image.x = -image.width / 2.0f;
 +image.y = -image.height / 2.0f;
 +[sprite addChild:​image];​
 +
 +sprite.rotation = SP_D2R(45.0f);​ // -> rotate around center
 +</​code>​
 +
 +Not a big deal either, Flash users do that all the time. But one might argue that it's a lot of code for such a simple thing. With the pivot point, the code is reduced to the following:
 +
 +<code objc>
 +SPImage *image = [SPImage imageWithContentsOfFile:​@"​texture.png"​];​
 +image.pivotX = image.width / 2.0f;
 +image.pivotY = image.height / 2.0f;
 +image.rotation = SP_D2R(45.0f);​ // -> rotate around center
 +</​code>​
 +
 +No more container sprite needed! To stick with the analogy used in previous chapters: the pivot point defines the position where you stab the pin through the object when you attach it to its parent. The code above moves the pivot point to the center of the object.
 +
 +{{ :​manual:​pivot_point.png?​ |Note how moving the pivot point changes how the object rotates!}}
 +
 +-----
 +
 +//Next section: [[Macros]]//​
  
  manual/pivot_points.txt · Last modified: 2013/05/25 16:13 by daniel
 
Powered by DokuWiki