[英]Passing Data Between Scenes (SpriteKit)
How do I pass information in SpriteKit from one scene to another? 如何将SpriteKit中的信息从一个场景传递到另一个场景? In my game I have two scenes, the GameScene and the GameOverScene.
在我的游戏中,我有两个场景,即GameScene和GameOverScene。 The score is displayed in the GameScene as it increases, but how do I send this information to the second scene?
分数随着增加而显示在GameScene中,但是如何将这些信息发送到第二个场景?
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)
}
} } }}
For instance, your GameOverScene
could be something like this: 例如,您的
GameOverScene
可能是这样的:
class GameOverScene: SKScene {
var object: SomeObject!
}
Now, in your changeScene: 现在,在您的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)
}
Subclass SKScene and create a new initializer with arguments that you want to pass. 子类化SKScene并使用要传递的参数创建一个新的初始化程序 。 As you transition from GameScene to second scene, create second scene by using the initializer you created and pass the score.
从GameScene过渡到第二场景时,请使用创建的初始化程序创建第二场景并传递分数。 Then present the second scene.
然后呈现第二个场景。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.