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?
所以我想知道,默认的“游戏”Xcode配置。你会如何制作另一款SKScene?就像我们说我想要一个菜单或什么?
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?
基本上我将如何创建另一个标题为“title.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.
我无法在重新编码的代码中找到它实际上告诉swift显示用“游戏”配置预制的游戏场景。
I figure I make a new class and have it inherit SKScene.
我想我创建了一个新类并让它继承了SKScene。
2 个解决方案
#1
2
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:
您可以通过继承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)
}
}
You can improve this by adding a method for custom transitions to the SceneTransitionDelegate.
您可以通过向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)
}
I made a quick demo xcode project here.
我在这里做了一个快速演示xcode项目。
#2
2
To make a new scene you simply create a new file for a scene class (look under iOS, then resources, you will see a sprite kit scene template). Now, to switch between scenes go back in your code and when you want to switch between them you would write this:
要创建一个新场景,您只需为场景类创建一个新文件(在iOS下查看,然后在资源中,您将看到一个精灵套件场景模板)。现在,要在场景之间切换,请回到代码中,当您想要在它们之间切换时,您会写下:
In Swift:
let scene = GameScene(size: CGSizeMake(1024,768))
scene.scaleMode = SKSceneScaleMode.AspectFill
let skView = self.view as SKView
let transition = SKTransition.flipVerticalWithDuration(0.5)
skView.presentScene(scene, transition: transition)
Or in Objective-C:
或者在Objective-C中:
MenuScene *nextScene = [[GameScene alloc] initWithSize:CGSizeMake(1024, 768)];
nextScene.scaleMode = SKSceneScaleModeAspectFill;
SKTransition *transition = [SKTransition flipVerticalWithDuration:0.5];
[self.view presentScene:nextScene transition:transition];
Here's a brief explanation of each line.
First, you are initializing a scene, and specifying its size.
以下是每行的简要说明。首先,您要初始化场景并指定其大小。
let scene = GameScene(size: CGSizeMake(1024,768))
This is the scene size by default, in case you were wondering about the numbers.
这是默认情况下的场景大小,以防您想知道这些数字。
Then you need to specify its scaleMode, which by default is aspect fill (thats also the one I use)
然后你需要指定它的scaleMode,默认情况下是方面填充(也就是我使用的那个)
scene.scaleMode = SKSceneScaleMode.AspectFill
Next, you can have a transition which is basically a fancy way of animating from one scene to the other
接下来,您可以进行转换,这种转换基本上是从一个场景到另一个场景的动画方式
let transition = SKTransition.flipVerticalWithDuration(0.5)
Finally, you use your SKView
(self.view
) to present your new scene.
最后,使用SKView(self.view)呈现新场景。
skView.presentScene(scene, transition: transition)
#1
2
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:
您可以通过继承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)
}
}
You can improve this by adding a method for custom transitions to the SceneTransitionDelegate.
您可以通过向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)
}
I made a quick demo xcode project here.
我在这里做了一个快速演示xcode项目。
#2
2
To make a new scene you simply create a new file for a scene class (look under iOS, then resources, you will see a sprite kit scene template). Now, to switch between scenes go back in your code and when you want to switch between them you would write this:
要创建一个新场景,您只需为场景类创建一个新文件(在iOS下查看,然后在资源中,您将看到一个精灵套件场景模板)。现在,要在场景之间切换,请回到代码中,当您想要在它们之间切换时,您会写下:
In Swift:
let scene = GameScene(size: CGSizeMake(1024,768))
scene.scaleMode = SKSceneScaleMode.AspectFill
let skView = self.view as SKView
let transition = SKTransition.flipVerticalWithDuration(0.5)
skView.presentScene(scene, transition: transition)
Or in Objective-C:
或者在Objective-C中:
MenuScene *nextScene = [[GameScene alloc] initWithSize:CGSizeMake(1024, 768)];
nextScene.scaleMode = SKSceneScaleModeAspectFill;
SKTransition *transition = [SKTransition flipVerticalWithDuration:0.5];
[self.view presentScene:nextScene transition:transition];
Here's a brief explanation of each line.
First, you are initializing a scene, and specifying its size.
以下是每行的简要说明。首先,您要初始化场景并指定其大小。
let scene = GameScene(size: CGSizeMake(1024,768))
This is the scene size by default, in case you were wondering about the numbers.
这是默认情况下的场景大小,以防您想知道这些数字。
Then you need to specify its scaleMode, which by default is aspect fill (thats also the one I use)
然后你需要指定它的scaleMode,默认情况下是方面填充(也就是我使用的那个)
scene.scaleMode = SKSceneScaleMode.AspectFill
Next, you can have a transition which is basically a fancy way of animating from one scene to the other
接下来,您可以进行转换,这种转换基本上是从一个场景到另一个场景的动画方式
let transition = SKTransition.flipVerticalWithDuration(0.5)
Finally, you use your SKView
(self.view
) to present your new scene.
最后,使用SKView(self.view)呈现新场景。
skView.presentScene(scene, transition: transition)