J.D*_*Doe 5 xcode sprite-kit swift
所以我想知道,默认的"游戏"Xcode配置.你会如何制作另一款SKScene?比如说我想要一个菜单或什么?
基本上我将如何创建另一个标题为"title.swift"的文档,然后以编程方式来回切换它们?这样我可以从菜单到游戏场景而不会弄乱我的代码?
我无法在重新编码的代码中找到它实际上告诉swift显示用"游戏"配置预制的游戏场景.
我想我创建了一个新类并继承了SKScene.
您可以通过子类化 SKScene 或您创建的任何其子类来创建新场景。您可以通过调用SKView方法presetScene(scene:SKScene)来切换场景。这就是我在上一款游戏中实现它的方式:
//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)
}
}
Run Code Online (Sandbox Code Playgroud)
您可以通过向 SceneTransitionDelegate 添加自定义过渡的方法来改进这一点。
protocol SceneTransitionDelegate {
func transitionToScene(sceneClass:Scene.Type)
func transitionToScene(sceneClass:Scene.Type, transitionAnimation:SKTransition)
}
Run Code Online (Sandbox Code Playgroud)
并将新方法添加到视图控制器,如下所示:
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)
}
Run Code Online (Sandbox Code Playgroud)
并从这样的场景中调用它:
func gameOverAnimated() {
let transition = SKTransition.crossFadeWithDuration(0.5)
sceneDelegate.transitionToScene(MainMenuScene.self, transitionAnimation: transition)
}
Run Code Online (Sandbox Code Playgroud)
我在这里做了一个快速演示 xcode 项目。
| 归档时间: |
|
| 查看次数: |
204 次 |
| 最近记录: |