- Swift Game Development(Third Edition)
- Siddharth Shekar Stephen Haney
- 276字
- 2021-07-16 17:37:43
Building a SKSpriteNode class
Let's begin by drawing a blue square on the screen. The SKSpriteNode
class can draw both texture graphics and solid blocks of color. It is often helpful to prototype your new game ideas with blocks of color before you spend time on artwork. To draw the blue square, add an instance of SKSpriteNode
to the game:
Override func didMove(to view: SKView) { // Make the scene position from its lower left // corner, regardless of any other settings: self.anchorPoint = .zero // Instantiate a constant, mySprite, instance of SKSpriteNode // The SKSpriteNode constructor can set color and size // Note: UIColor is a UIKit class with built-in colorpresets // Note: CGSize is a type we use to set node sizes let mySprite = SKSpriteNode(color: .blue, size: CGSize(width: 50, height: 50)) // Assign our sprite a position in points, relative to its // parent node (in this case, the scene) mySprite.position = CGPoint(x: 150, y: 150) // Finally, we need to add our sprite node into the node tree. // Call the SKScene'saddChild function to add the node // Note: In Swift, 'self' is an automatic property // on any type instance, exactly equal to the instance itself // So in this case, it refers to the GameScene instance self.addChild(mySprite) }
Go ahead and run the project. You should see a similar small blue square appear in your simulator:

Tip
Swift allows you to define variables as constants, which can be assigned a value only once. For the best performance, use let to declare constants whenever possible. Declare your variables with var when you need to alter the value later in your code.
推薦閱讀
- 智慧場(chǎng)館與智慧學(xué)習(xí)
- 蒲城文獻(xiàn)征錄
- 高校圖書(shū)館閱讀推廣與宣傳促進(jìn)研究
- 檔案檢索: 理論與方法
- 醫(yī)藥信息處理與分析
- 李一氓文存(第二卷):存在集三編下冊(cè)
- 知中16·西南聯(lián)大的遺產(chǎn)
- Swift Game Development(Third Edition)
- 校勘學(xué)釋例(中國(guó)文化叢書(shū)·經(jīng)典隨行)
- Rust Programming By Example
- 基于價(jià)值全面實(shí)現(xiàn)的檔案信息資源配置
- 顧頡剛?cè)a(bǔ)遺:顧頡剛?cè)?/a>
- 圖書(shū)館服務(wù)管理內(nèi)化:概念、過(guò)程及整合因素
- 高校圖書(shū)館創(chuàng)新服務(wù)(谷臻小簡(jiǎn)·AI導(dǎo)讀版)
- 讓文明得到更好的傳承:公共文化服務(wù)體系中公共檔案館發(fā)展戰(zhàn)略