Sam Acquaviva
Sam Acquaviva

Reputation: 137

Passing Data Between Scenes (SpriteKit)

How do I pass information in SpriteKit from one scene to another? In my game I have two scenes, the GameScene and the GameOverScene. The score is displayed in the GameScene as it increases, but how do I send this information to the second scene?

This function is called when the player runs out of lives which changes the scene.

 func changeScene(){

    let secondScene = GameOverScene(size: self.size)
    secondScene.scaleMode = scaleMode
    let transition = SKTransition.fadeWithDuration(0.5)
    self.view?.presentScene(secondScene, transition: transition)
}

This is my gameOverScene

class GameOverScene: SKScene {

var playAgainLabel = SKLabelNode(fontNamed:"Chalkduster")
var currentScore: Int
var scoreLabel = SKLabelNode(fontNamed: "Chalkduster")
var highScore = 0
var highScoreLabel = SKLabelNode(fontNamed: "Chalkduster")
var gameScene = GameScene()
override func didMoveToView(view: SKView) {

    backgroundColor = SKColor.blackColor()

    playAgainLabel.text = "Click To Play Again!"
    playAgainLabel.fontSize = 30
    playAgainLabel.fontColor = SKColor.whiteColor()
    playAgainLabel.position = CGPoint(x: frame.width / 2, y: frame.height / 2)
    self.addChild(playAgainLabel)

    if currentScore > highScore {
        highScore = currentScore
    }

    scoreLabel.text = "Score: " + String(currentScore)
    scoreLabel.fontSize = 20
    scoreLabel.fontColor = SKColor.whiteColor()
    scoreLabel.position = CGPoint(x: frame.width/2, y: frame.height/1.4)
    self.addChild(scoreLabel)

    highScoreLabel.text = "Best: " + String(highScore)
    highScoreLabel.fontSize = 20
    highScoreLabel.fontColor = SKColor.whiteColor()
    highScoreLabel.position = CGPoint(x: frame.width / 2, y: frame.height / 1.6)
    self.addChild(highScoreLabel)

}


override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    for touch in (touches as! Set<UITouch>) {
        let location = touch.locationInNode(self)

    let playingScene = GameScene(size: self.size)
    playingScene.scaleMode = scaleMode
    let fadeTransition = SKTransition.fadeWithDuration(0.5)
    self.view?.presentScene(playingScene, transition: fadeTransition)
}

} }

Upvotes: 1

Views: 2748

Answers (2)

Kelvin Lau
Kelvin Lau

Reputation: 6781

For instance, your GameOverScene could be something like this:

class GameOverScene: SKScene {
    var object: SomeObject!
}

Now, in your changeScene:

func changeScene(){
    let secondScene = GameOverScene(size: self.size)
    secondScene.scaleMode = scaleMode

    secondScene.object = somethingInFirstSceneThatNeedToBePassed //here we do the passing

    let transition = SKTransition.fadeWithDuration(0.5)
    self.view?.presentScene(secondScene, transition: transition)
}

Upvotes: 11

neo
neo

Reputation: 2042

Subclass SKScene and create a new initializer with arguments that you want to pass. As you transition from GameScene to second scene, create second scene by using the initializer you created and pass the score. Then present the second scene.

Upvotes: 0

Related Questions