- Game Development with Swift
- Stephen Haney
- 356字
- 2021-07-16 13:45:40
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.
- 企業大數據系統構建實戰:技術、架構、實施與應用
- PySpark大數據分析與應用
- UDK iOS Game Development Beginner's Guide
- 數亦有道:Python數據科學指南
- MySQL 8.x從入門到精通(視頻教學版)
- 基于OPAC日志的高校圖書館用戶信息需求與檢索行為研究
- 深入淺出 Hyperscan:高性能正則表達式算法原理與設計
- SAS金融數據挖掘與建模:系統方法與案例解析
- Unreal Engine Virtual Reality Quick Start Guide
- SIEMENS數控技術應用工程師:SINUMERIK 840D-810D數控系統功能應用與維修調整教程
- Spring MVC Beginner’s Guide
- AndEngine for Android Game Development Cookbook
- 數據之美:一本書學會可視化設計
- 推薦系統全鏈路設計:原理解讀與業務實踐
- Learn Selenium