昨天介紹完 SpriteKit 的建檔,今天會介紹程式碼的部分
GameScene.sks
遊戲場景的格局設置
GameViewController.swift
mport UIKit
mport SpriteKit // 創建遊戲和其他圖形密集型應用程序,在二維中繪製形狀、粒子、文本、圖像和視頻
mport GameplayKit // 融入常見的遊戲行為,例如隨機數生成、人工智能、尋路和代理行為。
lass GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if let view = self.view as! SKView? {
// 從 "GameScene.sks" 加载 "SKScene"
if let scene = SKScene(fileNamed: "GameScene") {
// 將縮放模式設置為適合裝置的縮放比例
scene.scaleMode = .aspectFill
// 呈現場景
view.presentScene(scene)
}
// 忽略遍歷順序,需要在場景中使用 zPosition,以正確保證元素在彼此的前面或後面
view.ignoresSiblingOrder = true
// 顯示性能統計信息(fps、nodes)
view.showsFPS = true
view.showsNodeCount = true
}
}
// 是否自動旋轉
override var shouldAutorotate: Bool {
return true
}
// UI 界面的遮罩
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
// 使用者介面
if UIDevice.current.userInterfaceIdiom == .phone {
return .allButUpsideDown
} else {
return .all
}
}
// 狀態欄隱藏
override var prefersStatusBarHidden: Bool {
return true
}
``
GameScene.swift
mport SpriteKit
mport GameplayKit
lass GameScene: SKScene {
private var label : SKLabelNode?
private var spinnyNode : SKShapeNode?
override func didMove(to view: SKView) {
// 從場景中獲取標籤節點
self.label = self.childNode(withName: "//helloLabel") as? SKLabelNode
if let label = self.label {
label.alpha = 0.0
label.run(SKAction.fadeIn(withDuration: 2.0))
}
// 創建游標在裝置上呈現的的形狀節點
let w = (self.size.width + self.size.height) * 0.05
self.spinnyNode = SKShapeNode.init(rectOf: CGSize.init(width: w, height: w), cornerRadius: w * 0.3)
if let spinnyNode = self.spinnyNode {
// 節點線條的寬度
spinnyNode.lineWidth = 2.5
// 節點的動畫效果呈現
spinnyNode.run(SKAction.repeatForever(SKAction.rotate(byAngle: CGFloat(Double.pi), duration: 1)))
spinnyNode.run(SKAction.sequence([SKAction.wait(forDuration: 0.5),
SKAction.fadeOut(withDuration: 0.5),
SKAction.removeFromParent()]))
}
}
// 按下時,會出現綠色節點
func touchDown(atPoint pos : CGPoint) {
if let n = self.spinnyNode?.copy() as! SKShapeNode? {
n.position = pos
n.strokeColor = SKColor.green
self.addChild(n)
}
}
// 拖曳時,會出現藍色節點
func touchMoved(toPoint pos : CGPoint) {
if let n = self.spinnyNode?.copy() as! SKShapeNode? {
n.position = pos
n.strokeColor = SKColor.blue
self.addChild(n)
}
}
// 放開後,會出現紅色節點
func touchUp(atPoint pos : CGPoint) {
if let n = self.spinnyNode?.copy() as! SKShapeNode? {
n.position = pos
n.strokeColor = SKColor.red
self.addChild(n)
}
}
// 按下後,畫面上的 "Hello, World!" 會閃爍一下
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let label = self.label {
label.run(SKAction.init(named: "Pulse")!, withKey: "fadeInOut")
}
for t in touches { self.touchDown(atPoint: t.location(in: self)) }
}
// 拖曳
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for t in touches { self.touchMoved(toPoint: t.location(in: self)) }
}
// 游標放開
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
for t in touches { self.touchUp(atPoint: t.location(in: self)) }
}
// 游標取消
override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
for t in touches { self.touchUp(atPoint: t.location(in: self)) }
}
// 更新
override func update(_ currentTime: TimeInterval) {
// 畫面每一幀前皆會被調用
}
``