官术网_书友最值得收藏!

The story on positioning

SpriteKit uses a grid of points to position nodes. In this grid, the bottom left corner of the scene is (0,0), with a positive X-axis to the right and a positive Y-axis to the top.

Similarly, on the inpidual sprite level, (0,0) refers to the bottom left corner of the sprite, while (1,1) refers to the top right corner.

Alignment with anchor points

Each sprite has an anchorPoint property, or an origin. The anchorPoint property allows you to choose which part of the sprite aligns to the sprite's overall position.

Note

The default anchor point is (0.5,0.5), so a new SKSpriteNode centers perfectly on its position.

To illustrate this, let us examine the blue square sprite we just drew on the screen. Our sprite is 50 pixels wide and 50 pixels tall, and its position is (300,300). Since we have not modified the anchorPoint property, its anchor point is (0.5,0.5). This means the sprite will be perfectly centered over the (300,300) position on the scene's grid. Our sprite's left edge begins at 275 and the right edge terminates at 325. Likewise, the bottom starts at 275 and the top ends at 325. The following diagram illustrates our block's position on the grid:

Why do we prefer centered sprites by default? You may think it simpler to position elements by their bottom left corner with an anchorPoint property setting of (0,0). However, the centered behavior benefits us when we scale or rotate sprites:

  • When we scale a sprite with an anchorPoint property of (0,0) it will only expand up the y-axis and out the x-axis. Rotation actions will swing the sprite in wide circles around its bottom left corner.
  • A centered sprite, with the default anchorPoint property of (0.5, 0.5), will expand or contract equally in all directions when scaled and will spin in place when rotated, which is usually the desired effect.

There are some cases when you will want to change an anchor point. For instance, if you are drawing a rocket ship, you may want the ship to rotate around the front nose of its cone, rather than its center.

主站蜘蛛池模板: 静乐县| 新丰县| 鄢陵县| 凤冈县| 房产| 木里| 广元市| 巴东县| 巴彦县| 从江县| 广丰县| 嫩江县| 卫辉市| 崇仁县| 策勒县| 嘉禾县| 包头市| 刚察县| 肥城市| 鄂托克前旗| 淄博市| 固阳县| 成武县| 镇原县| 休宁县| 姜堰市| 襄樊市| 三穗县| 晋州市| 施甸县| 千阳县| 托克托县| 盐亭县| 洛扎县| 东乌| 呼伦贝尔市| 霍邱县| 常熟市| 三门县| 建瓯市| 宣恩县|