Passing Data Between Scenes (SpriteKit)

北城以北 提交于 2019-12-31 05:24:18

问题


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)
}

} }


回答1:


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)
}



回答2:


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.



来源:https://stackoverflow.com/questions/31680355/passing-data-between-scenes-spritekit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!