iPhone Swift 改变你的初始场景
iPhone Swift Changing your initial Scene
现在,我知道如何在 SpriteKit 游戏中转换场景了。我无法解决的棘手问题是更改加载的初始文件。我创建的游戏直接跳入游戏。那个最初的场景叫做 GameScene.swift。我创建了一个 MainMenuScene.swift 并且我正在尝试将其更改为先加载它。然后一旦单击,我将对下一个场景执行 SKTransition,就像我稍后在游戏中所做的那样。
这是我的视图控制器代码:
override func viewDidLoad() {
super.viewDidLoad()
if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene {
// 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)
}
}
我认为将 "GameScene" 更改为 "MainMenuScene" 可以解决问题,但根本无法加载任何内容。然后我想也许是大小问题,但所有设置都与另一个场景相同。即使尝试添加节点数,也没有显示任何内容。所以我假设根本没有加载任何东西。也许我又瞎又笨,但我就是看不到。
提前致谢。
在你的 viewcontroller 中试试这个
// 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
let scene = MainMenuScene(size: skView.frame.size)
/* Set the scale mode to scale to fit the window */
scene.scaleMode = .AspectFill
skView.presentScene(scene)
在 MainMenuScene 中,确保添加这些初始化程序
override init(size: CGSize) {
super.init(size: size)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
让你的 viewController 像这样:
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let scene = MainMenuScene(size: view.bounds.size) //This can be any scene you want the game to load first
let skView = self.view as! SKView
skView.showsFPS = false
skView.showsNodeCount = false
skView.ignoresSiblingOrder = true
scene.scaleMode = .ResizeFill
skView.presentScene(scene)
}
}
在您的 MainMenuScene.swift(以及您希望包含在游戏中的所有其他场景)中,确保 class 本身是这样开始的:
class chooseScene: SKScene {
override func didMoveToView(view: SKView) {
} }
这是我在 viewController 和 SKScenes 中使用的代码,几个月前我遇到了与您完全相同的问题。
现在,我知道如何在 SpriteKit 游戏中转换场景了。我无法解决的棘手问题是更改加载的初始文件。我创建的游戏直接跳入游戏。那个最初的场景叫做 GameScene.swift。我创建了一个 MainMenuScene.swift 并且我正在尝试将其更改为先加载它。然后一旦单击,我将对下一个场景执行 SKTransition,就像我稍后在游戏中所做的那样。
这是我的视图控制器代码:
override func viewDidLoad() {
super.viewDidLoad()
if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene {
// 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)
}
}
我认为将 "GameScene" 更改为 "MainMenuScene" 可以解决问题,但根本无法加载任何内容。然后我想也许是大小问题,但所有设置都与另一个场景相同。即使尝试添加节点数,也没有显示任何内容。所以我假设根本没有加载任何东西。也许我又瞎又笨,但我就是看不到。
提前致谢。
在你的 viewcontroller 中试试这个
// 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
let scene = MainMenuScene(size: skView.frame.size)
/* Set the scale mode to scale to fit the window */
scene.scaleMode = .AspectFill
skView.presentScene(scene)
在 MainMenuScene 中,确保添加这些初始化程序
override init(size: CGSize) {
super.init(size: size)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
让你的 viewController 像这样:
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let scene = MainMenuScene(size: view.bounds.size) //This can be any scene you want the game to load first
let skView = self.view as! SKView
skView.showsFPS = false
skView.showsNodeCount = false
skView.ignoresSiblingOrder = true
scene.scaleMode = .ResizeFill
skView.presentScene(scene)
}
}
在您的 MainMenuScene.swift(以及您希望包含在游戏中的所有其他场景)中,确保 class 本身是这样开始的:
class chooseScene: SKScene {
override func didMoveToView(view: SKView) {
} }
这是我在 viewController 和 SKScenes 中使用的代码,几个月前我遇到了与您完全相同的问题。