所以我想知道,默认的“游戏”
Xcode配置.你会如何制作另一款SKScene?比如说我想要一个菜单或什么?
基本上我将如何创建另一个标题为“title.swift”的文档,然后以编程方式来回切换它们?这样我可以从菜单到游戏场景而不会弄乱我的代码?
我无法在重新编码的代码中找到它实际上告诉swift显示用“游戏”配置预制的游戏场景.
我想我创建了一个新类并继承了SKScene.
解决方法
您可以通过继承SKScene或其创建的任何子类来创建新场景.您可以通过调用SKView方法presetScene(场景: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) } }
您可以通过向SceneTransitionDelegate添加自定义过渡的方法来改进这一点.
protocol SceneTransitionDelegate { func transitionToScene(sceneClass:Scene.Type) func transitionToScene(sceneClass:Scene.Type,transitionAnimation:SKTransition) }
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) }
func gameOverAnimated() { let transition = SKTransition.crossFadeWithDuration(0.5) sceneDelegate.transitionToScene(MainMenuScene.self,transitionAnimation: transition) }