Swift Multiple Level Scenes

Swift multiple level scenes

You can override functions of baseScene in level1Scene, you just need to make sure you call the super version of the method. Here are a few examples, in your level1Scene class:

override func didMoveToView(view: SKView) {
super.didMoveToView(view) // Calls `didMoveToView` of `baseScene`.

// Additional setup needed for `level1Scene`...
}

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
super.touchesBegan(touches, withEvent: event) // Calls `touchesBegan` of `baseScene`.

// Additional stuff you want to do in `level1Scene`...
}

Sprite Kit - Defining the variables for multiple scenes

1 - Subclassing is the answer. Create a class BaseScene, which is a subclass of SKScene. Include all the common elements of all the scenes here. This includes not only the bitmask categories, but also any methods or other properties that the scenes may have. This will improve the length of your code besides solving your problems.

Make all your level scenes a subclass of BaseScene.

2 - Create a Bool variable called scoreReached or something in your code. Set this variable to NO while initialising, and then in the code where you check your score within the -update method, do something like this:

if (!scoreReached)
{
if (score >= 100)
{
//Do whatever is needed
scoreReached = YES;
}
}

Reuse same sprite node within multiple scenes in sprite kit using Swift

There is a few ways to do this.

You could subclass your other scenes to be subclass of the scene with the loadNode function which gives those scenes access to that function.

I asked a question about this last year

Swift multiple level scenes

Another way that might be a bit easier if you are not comfortable with scene subclassing is to just create a subclass of the node itself.

So you create a class

enum EnemyType {
case Normal
case Special
}

class NodeA1: SKSpriteNode {

init(imageNamed: String, enemyType: EnemyType) {
let texture = SKTexture(imageNamed: imageNamed)

if enemyType == .Normal {
super.init(texture: texture, color: SKColor.clearColor(), size: texture.size())
else {
// other init
}

self.zPosition = 1
self.name = ""
// add physics body, other properties or methods for the node

}
}

Than in your SKScenes you can add the node in the init method like so

  nodeA1 = NodeA1(imageNamed: "ImageName", enemyType: .Normal)
nodeA1.position = ....
addChild(nodeA1)

this way ever scene where you add the node will use the subclass and therefore include all the properties, set up etc for that node. Another benefit with subclassing is that you could loop through all your nodes using

self.enumerateChildNodesWithName...

and than call custom methods on all nodes.

If you want to subclass your scenes than you would create your baseScene

 class BaseScene: SKScene {

// set up all shared stuff in didMoveToView
// have your node function here
// touches began
// physics word and contact collision
// all other stuff that needs to be shared between all level scenes

}

Than your subsequent level scenes would look something like this

  class Level1Scene: BaseScene {

override func didMoveToView(view: SKView) {
super.didMoveToView(view) // This lines imports all stuff in BaseScene didMoveToView

// do level 1 specific setUps.
// you can call any function or property from BaseScene, e.g the loadNode function.
}

You than load you level scenes as usual, e.g you transition to level 1 scene and it will automatically use/have access to all the superclass methods and sprites (BaseScene).
So you never call baseScene directly, its gets called automatically.

This applies for other methods in baseScene too, so say you have a Update method in BaseScene.

 override func update(currentTime: CFTimeInterval) {.... }

This will work across all your level scenes which are subclasses of BaseScene.

But what happens if you need to add some specific stuff to the update method only relevant in 1 level scene and not all level scenes?
It would be the same process, you create a new update func in the LevelScene and call super.

 override func update(currentTime: CFTimeInterval) {
super.update(currentTime) // this calls the baseScene Update method

/// specific stuff for that level only
}

Super simply means the super class of the currentScene, which is BaseScene if the scene is a subclass of it.

Is this helping?

SceneKit – Display multiple scenes in same SCNView

Ok I already figured it out. The thing is to not add another scene but nodes from the 2nd scene to the first like this:

let scene = SCNScene(named: "world1.dae")!
let subScene = SCNScene(named: "pyramid.dae")!

let pyramid = subScene.rootNode.childNodeWithName("pyramid", recursively: true)!

scene.rootNode.addChildNode(pyramid)

Swift/SceneKit - best practice to create menu / level select scenes for the game

“the nodePressed in overlayScene still contains the playButton node...”

You are setting the pointer to the overlay scene to nil, and in touchesbegan you get the location from the overlay scene but then you use atPoint on the skscene stored in the “controls” property, so it still finds the play button. So replace self.controls with scnView.overlaySKScene in touchesbegan.

That said, I don’t know what in general is considered best practice by others but personally, once I need more than just a couple of buttons, I stop using the spritekit overlay scene and instead use regular UIKit elements to build menus on top of the SCNView.



Related Topics



Leave a reply



Submit