- Swift Game Development(Third Edition)
- Siddharth Shekar Stephen Haney
- 353字
- 2021-07-16 17:37:43
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 individual 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's examine the blue square sprite we just drew on the screen. Our sprite is 50 points wide and 50 points tall, and its position is (150,150). Since we have not modified the anchorPoint
property, its anchor point is (0.5,0.5). This means that the sprite will be perfectly centered over the (150,150) position on the scene's grid. Our sprite's left edge begins at 125 and the right edge terminates at 175. Likewise, the bottom starts at 125 and the top ends at 175. The following diagram illustrates our block's position on the grid:

Why do we prefer centered sprites by default? You may think it's 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 a bottom-left
anchorPoint
property of (0,0), it will only expand up the y-axis and across 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 where 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.
- 單讀. 十周年特輯(時(shí)間的移民+在世界的門外)共2冊(cè)
- 校勘學(xué)大綱
- 檔案社會(huì)控制功能研究
- 圖書館服務(wù)思維研究
- Learning Robotics using Python
- 科學(xué)名家
- 中國(guó)人民大學(xué)“復(fù)印報(bào)刊資料”轉(zhuǎn)載指數(shù)排名研究報(bào)告(2014)
- 現(xiàn)代圖書館全面質(zhì)量管理與創(chuàng)新服務(wù)研究
- 李一氓文存(第二卷):存在集三編下冊(cè)
- 守望與思索:人文清華講壇實(shí)錄III
- 基于知識(shí)治理的競(jìng)爭(zhēng)情報(bào)協(xié)同模型研究
- 圖書情報(bào)與圖書館服務(wù)探究
- 圖書館戰(zhàn)略規(guī)劃研究
- 圖書館服務(wù)管理內(nèi)化:概念、過(guò)程及整合因素
- 北疆博物院的故事