如何在我的应用程序上创建另一个屏幕 [英] How to create another screen on my app
问题描述
所以我想知道,使用默认的游戏"Xcode 配置.你会如何制作另一个 SKScene?比如说我想要一个菜单什么的?
So I was wondering, with the default "game" Xcode configuration. How would you make another SKScene? Like lets say I wanted to have a menu or something?
基本上我将如何创建另一个名为title.swift"的文档,然后以编程方式在它们之间来回切换?这样我就可以从菜单转到游戏场景而不会弄乱我的代码?
Basically how would I create another document titled "title.swift" and then programmatically be able to switch back and forth through them? That way I could go from a menu to the game scene without muddling up my code?
我无法在重新制作的代码中找到它实际上告诉 swift 显示使用游戏"配置预先制作的游戏场景的位置.
I cant find where in the remade code code it actually tell swift to display the game scene that was premade with the "game" configuration.
我想我创建了一个新类并让它继承了 SKScene.
I figure I make a new class and have it inherit SKScene.
推荐答案
您可以通过继承 SKScene 或其任何您创建的子类来创建新场景.可以通过调用SKView方法presetScene(scene:SKScene)来切换场景.这是我在上一场比赛中实现它的方式:
You can create new scenes by subclassing SKScene or any of its subclasses you create. You can switch between scenes by calling the SKView method presetScene(scene:SKScene). This is how I implemented it in my last game:
//handles all scene transitions
protocol SceneTransitionDelegate {
//this passes the scene as a class (not object)
func transitionToScene(sceneClass:Scene.Type)
}
//make sure all you scenes are a subclass of this (so they inherit sceneDelegate):
class Scene: SKScene { //Scene inherits from SKScene
var sceneDelegate:SceneTransitionDelegate!
required override init(size: CGSize) {
super.init(size: size)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
class GameScene:Scene {
//call this method within the scene to return to the main menu
func gameOver() {
sceneDelegate.transitionToScene(MainMenuScene.self)
}
}
class MainMenuScene: Scene {
//call this method to transition to a game scene
func newGame() {
sceneDelegate.transitionToScene(GameScene.self)
}
}
//the VC that presents your scenes
class GameViewController: UIViewController, SceneTransitionDelegate {
var skView:SKView { return view as! SKView }
override func viewDidLoad() {
super.viewDidLoad()
//load first scene (e.g. MainMenu)
transitionToScene(MainMenuScene.self)
}
//SceneTransitionDelegate method
func transitionToScene(sceneClass:Scene.Type) {
let scene = sceneClass(size: skView.bounds.size)
scene.scaleMode = .AspectFill
scene.sceneDelegate = self
skView.presentScene(scene)
}
}
您可以通过向 SceneTransitionDelegate 添加自定义过渡方法来改进这一点.
You can improve this by adding a method for custom transitions to the SceneTransitionDelegate.
protocol SceneTransitionDelegate {
func transitionToScene(sceneClass:Scene.Type)
func transitionToScene(sceneClass:Scene.Type, transitionAnimation:SKTransition)
}
并将新方法添加到视图控制器,如下所示:
and add the new method to the view controller like this:
func transitionToScene(sceneClass: Scene.Type, transitionAnimation: SKTransition) {
let scene = sceneClass(size: skView.bounds.size)
scene.scaleMode = .AspectFill
scene.sceneDelegate = self
skView.presentScene(scene, transition: transitionAnimation)
}
并从这样的场景中调用它:
and call it from within scenes like this:
func gameOverAnimated() {
let transition = SKTransition.crossFadeWithDuration(0.5)
sceneDelegate.transitionToScene(MainMenuScene.self, transitionAnimation: transition)
}
我在此处制作了一个快速演示 xcode 项目.
I made a quick demo xcode project here.
这篇关于如何在我的应用程序上创建另一个屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!