J. Derbs
J. Derbs

Reputation: 127

Passing Data between Scenes in SpriteKit

I've Googled this for about ten minutes, and everybody else's suggestions aren't working for me. I'm simply trying to pass the score of my game from GameScene to GameOver. I'm not using NSUserDefaults but am open to suggestions for it.

I've created my score variable above the didMove method.

var meteorScore = 0

and transition scenes by

let reveal = SKTransition.reveal(with: .down, duration: 1)
let gameOver = GameOver(size: self.size)
self.view?.presentScene(gameOver, transition: reveal)

I'm trying to access it like this

var score = GameScene(GameScene.level)

in GameOver, but I get a few errors. If there's a way to do this with NSUserDefaults instead of like this i'd be happy to switch my code around. Any ideas?

Upvotes: 0

Views: 388

Answers (2)

J. Derbs
J. Derbs

Reputation: 127

I figured it out with UserDefaults. For those who are just starting out with UserDefaults, there are a lot of really good videos to learn from.

Upvotes: 0

Ron Myschuk
Ron Myschuk

Reputation: 6061

why don't you try it the other way around.

create your meteorScore variable in GameOver

var meteorScore = 0

then when transitioning to that scene pass the variable to the scene

let reveal = SKTransition.reveal(with: .down, duration: 1)
let gameOver = GameOver(size: self.size)
gameOver.meteorScore = whateverScoreVariable
self.view?.presentScene(gameOver, transition: reveal)

this should do exactly what you're looking for

Upvotes: 2

Related Questions