问题
When using Xcode, I've faced the annoying problem, which is Xcode always crashes when I click .SKS files. I have raised questions here, and even in Apple developer forum, as well as searched for the solutions on the Internet... but it is hopeless.
Because I am making a game with many scenes, so if I can't use SpriteKit editor to interact with the scenes in an easy way, I want to know how can I interact with my scenes by coding their .swift files.
So the question is, for example, when I create a file "EndScene.sks", how can I create a "EndScene.swift", which links with my .sks file?
Thank you very much!
回答1:
Create a new swift file and name it the same as your .sks file. Let's say you have a .sks file called MainMenu.sks
. You'd create a swift file called MainMenu.swift
. Inside that file, you'll want to create a Main Menu class that inherits from SKScene
.
import SpriteKit
class MainMenu: SKScene {
}
Inside there is where you'll put all your code. The key is, as you said, linking this to the .sks file.
When you go to present your scene, you'll instantiate the class and associate it with the .sks file.
import UIKit
import SpriteKit
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if let scene = MainMenu(fileNamed:"MainMenu") {
let skView = self.view as! SKView
// Some settings applied to the scene and view
// ... code ...
skView.presentScene(scene)
}
}
//... other code
}
Note the line let scene = MainMenu(fileNamed:"MainMenu")
. That's where you are instantiating the class you created in MainMenu.swift.
So, technically, MainMenu()
is the .swift
file and fileNamed: "MainMenu"
is the .sks
file. You can technically put any .sks
file into the init call and it'll render that scene.
Imagine having a game with all the logic for a maze runner. You could build all the game logic in a class called MazeScene
and just make a bunch of .sks
files, each with a different maze. You could then so something like, MazeScene(fileNamed: "MazeOne")
or MazeScene(fileNamed: "MazeThree")
.
For the documentation on this, SKScene inherits from SKNode so you'll find the documentation for init(fileNamed:) here
That should get you going.
回答2:
So instead of commenting I have to wander because StackOverflow won't let me comment, how much of this is the same in swift 3? And when you were doing this in the UIViewController you said
if let scene = MainMenu(fileNamed:"MainMenu") {
let skView = self.view as! SKView
// Some settings applied to the scene and view
// ... code ...
skView.presentScene(scene)
}
What do you mean by
// ... code ...
Do we put all of the code we would put in the SKScene class in there instead? I thought the whole point was to have a separate SKScene class. And when I try it like that the SKScene class I have doesn't load. Thanks for any clarification and sorry I have to answer instead of comment. It's pretty stupid that you need 50 reputation to leave a comment I don't see any point in that. Anyway, Thanks!
来源:https://stackoverflow.com/questions/37394190/how-can-i-create-a-swift-file-for-my-new-sks-file