更改默认 SKScene
Changing default SKScene
我有一个名为 IntroLayer 的 class 我正在尝试在启动游戏时加载为初始场景。但是,按照这些简单步骤中的描述,简单地将 GameScene 更改为 IntroScene 后,我的 IntroScene 没有被加载。我在 if let 场景上设置断点以查看它是否跳过它,甚至在实际的 IntroScene 中设置断点以验证未调用 didMoveToView。有什么想法吗?
我在 GameViewController 中将 let 场景从 GameScene 更改为 IntroScene,如下所示:
import UIKit
import SpriteKit
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if let scene = IntroScene(fileNamed:"IntroScene") {
// Configure the view.
let skView = self.view as! SKView
skView.showsFPS = true
skView.showsNodeCount = true
/* Sprite Kit applies additional optimizations to improve rendering performance */
skView.ignoresSiblingOrder = true
/* Set the scale mode to scale to fit the window */
scene.scaleMode = .AspectFill
skView.presentScene(scene)
}
}
override func shouldAutorotate() -> Bool {
return true
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
return .AllButUpsideDown
} else {
return .All
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Release any cached data, images, etc that aren't in use.
}
override func prefersStatusBarHidden() -> Bool {
return true
}
}
然后将 GameScene.swift 重命名为 IntroScene.swift 并将 class 更改为:
import SpriteKit
class IntroScene: SKScene {
override func didMoveToView(view: SKView) {
backgroundColor = UIColor.blackColor()
let fadeIn:SKAction = SKAction.fadeInWithDuration(1.0)
let fadeOut:SKAction = SKAction.fadeOutWithDuration(1.0)
let inspiredText = SKLabelNode(fontNamed: "Dead Kansas")
inspiredText.alpha = 0.0
inspiredText.text = "Inspired By"
inspiredText.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame))
addChild(inspiredText)
inspiredText.runAction(fadeIn)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
/* Called when a touch begins */
for touch in touches {
// let location = touch.locationInNode(self)
}
}
}
然而,当我启动该应用程序时,似乎在我设置断点时甚至没有调用 IntroScene,而且我的屏幕背景为灰色。
您需要将 GameScene.sks 重命名为 IntroScene.sks
sks文件是场景数据的存档文件,如果你打开它,你会发现你可以改变场景的很多东西,甚至可以直接添加节点和精灵。当您调用 if let scene = IntroScene(fileNamed:"IntroScene") {
时,您正在解压缩 sks 文件以作为场景加载,因此无论何时您想要创建新场景,请记住您将需要此文件。
我有一个名为 IntroLayer 的 class 我正在尝试在启动游戏时加载为初始场景。但是,按照这些简单步骤中的描述,简单地将 GameScene 更改为 IntroScene 后,我的 IntroScene 没有被加载。我在 if let 场景上设置断点以查看它是否跳过它,甚至在实际的 IntroScene 中设置断点以验证未调用 didMoveToView。有什么想法吗?
我在 GameViewController 中将 let 场景从 GameScene 更改为 IntroScene,如下所示:
import UIKit
import SpriteKit
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if let scene = IntroScene(fileNamed:"IntroScene") {
// Configure the view.
let skView = self.view as! SKView
skView.showsFPS = true
skView.showsNodeCount = true
/* Sprite Kit applies additional optimizations to improve rendering performance */
skView.ignoresSiblingOrder = true
/* Set the scale mode to scale to fit the window */
scene.scaleMode = .AspectFill
skView.presentScene(scene)
}
}
override func shouldAutorotate() -> Bool {
return true
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
return .AllButUpsideDown
} else {
return .All
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Release any cached data, images, etc that aren't in use.
}
override func prefersStatusBarHidden() -> Bool {
return true
}
}
然后将 GameScene.swift 重命名为 IntroScene.swift 并将 class 更改为:
import SpriteKit
class IntroScene: SKScene {
override func didMoveToView(view: SKView) {
backgroundColor = UIColor.blackColor()
let fadeIn:SKAction = SKAction.fadeInWithDuration(1.0)
let fadeOut:SKAction = SKAction.fadeOutWithDuration(1.0)
let inspiredText = SKLabelNode(fontNamed: "Dead Kansas")
inspiredText.alpha = 0.0
inspiredText.text = "Inspired By"
inspiredText.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame))
addChild(inspiredText)
inspiredText.runAction(fadeIn)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
/* Called when a touch begins */
for touch in touches {
// let location = touch.locationInNode(self)
}
}
}
然而,当我启动该应用程序时,似乎在我设置断点时甚至没有调用 IntroScene,而且我的屏幕背景为灰色。
您需要将 GameScene.sks 重命名为 IntroScene.sks
sks文件是场景数据的存档文件,如果你打开它,你会发现你可以改变场景的很多东西,甚至可以直接添加节点和精灵。当您调用 if let scene = IntroScene(fileNamed:"IntroScene") {
时,您正在解压缩 sks 文件以作为场景加载,因此无论何时您想要创建新场景,请记住您将需要此文件。