Linking the SKS file to a Custom Class in SpriteKit
As you suggested, the SKS file is probably not linked to the swift file.
In Xcode
- open the
sks
file - select your scene
- open the right side panel
- open the
Custom Class Inspector
- type
MenuScene
into theCustom Class
field
Then try again.
How do I link a .sks file to a .swift file in SpriteKit
This method will unarchive an sks file and initialise it as whichever SKNode subclass it is called on. This means you can unarchive a file as an SKNode (so the root node will be an SKNode) and add it as a child to your scene. You can also unarchive a file as GameScene or any SKNode subclass.
extension SKNode {
class func unarchiveFromFile(file : NSString) -> SKNode? {
if let path = NSBundle.mainBundle().pathForResource(file, ofType: "sks") {
var sceneData = NSData(contentsOfFile: path, options: .DataReadingMappedIfSafe, error: nil)!
var archiver = NSKeyedUnarchiver(forReadingWithData: sceneData)
archiver.setClass(self.classForKeyedUnarchiver(), forClassName: "SKScene")
let scene = archiver.decodeObjectForKey(NSKeyedArchiveRootObjectKey) as SKNode
archiver.finishDecoding()
return scene
} else {
return nil
}
}
}
Use it like this:
let scene = GameScene.unarchiveFromFile("GameScene")! as GameScene
or
let levelStuff = SKNode.unarchiveFromFile("Level 1")!
self.addChild(levelStuff)
How can you connect a swift file with an SKscene?
You can do it like this:
1) Create .swift
file and make a subclass of a SKScene
Go to :
File menu -> New File -> Source -> Swift file
and make subclass of a SKScene
class MenuScene:SKScene {}
2) Create .sks file
Then go to
File menu -> New File -> Resource -> SpriteKit Scene
and make a MenuScene.sks
(name it MenuScene
without the actual extension).
Repeat this steps for each scene you want to have.
Then to load and start your initial scene. Do this inside your GameViewController.swift
:
if let scene = GameScene(fileNamed:"GameScene") {
let skView = self.view as! SKView
//setup your scene here
skView.presentScene(scene)
}
To make a transition to other scene (lets assume that you are in the MenuScene
currently) you should do something like this:
if let nextScene = GameScene(fileNamed: "GameScene"){
nextScene.scaleMode = self.scaleMode
let transition = SKTransition.fadeWithDuration(1)
view?.presentScene(nextScene, transition: transition)
}
How can I reference a GameScene.swift file as a base for the rest of my game levels?
Yes, you can subclass your original Gamescene and inherit all of its functionality from the other game scenes you are creating.
So you would create an original game scene that inherits from SKScene, like this:
class OriginalGameScene: SKScene {
// all of your shared logic here (didMoveTo and didBegin) functions
}
And for all the game scenes in your project, just inherit from that original game scene like this:
class FirstGameScene: OriginalGameScene {
// this class has the logic from the functions didMoveTo and didBegin
}
Only put the logic that you want shared throughout every other game scene in your OriginalGameScene
and you will be able to access any functions or logic that you implement in that OriginalGameScene
file.
Connecting multiple .sks files to multiple .swift files in SpriteKit
To connect .sks files to .swift files, you simply give them the same name. What I was missing was that I needed to create a "Cocoa Touch Class" instead of a new "Swift File." If the class and the .sks file have the same name, they are automatically connected.
Creating new swift scene with corresponding sks file
This can be done via initializing a new action.. check SKAction
documentation (or autocomplete in xcode) to see it's initializers.
Here is a snippet based on Apple's website that will do what you need:
// init?(named: String)
// Creates an action of the given name from an action file.
let action = SKAction(named: "whateverYourActionnameIS")
Scroll all the way to the bottom to see the list of functions, inits, etc, for SKAction:
https://developer.apple.com/reference/spritekit/skaction
Related Topics
Swift: Optional Text in Optional Value
How Replace Position++ Code to Make It Swift 3 Compatible
Why Can't You Assign an Optional to a Variable of Type 'Any' Without a Warning
Reading Currently Playing Track in MACos Using Scriptingbridge Not Working
How to Remove Optional from String Value Swift
Can the Conversion of a String to Data with Utf-8 Encoding Ever Fail
How Is Optional Binding Used in Swift
How to Get Random Element from a Set in Swift
Higher Order Function: "Cannot Invoke 'Map' with an Argument List of Type '((_) -> _)'"
How to Bind Different Associated Values in a Swift Enum to the Same Var
"Generic Parameter Could Not Be Inferred" in Swiftui Uiviewrepresentable
How to Use Raycast Methods in Realitykit
Simultaneous Gesture Recognition for Specific Gestures
Differencebetween Swift 2.0 Do-Try-Catch and Regular Java/C#/C++ Exceptions