简体   繁体   English

Swift-将游戏得分传递给第二视图控制器

[英]Swift - passing game score to second view controller

I'm trying to pass the highscore from my GameScene to my GameOverViewController . 我正在尝试将GameScene的高分传递给GameOverViewController I've successfully added the segue GameOver and the app works as expected. 我已经成功添加了segue GameOver并且该应用程序按预期工作。

The point of the GameOVerViewController is to display the score, advert and replay option. GameOVerViewController是显示得分,广告和重播选项。

I've looked at a number of tutorials however I seem to be hitting a wall. 我看过许多教程,但是我似乎碰壁了。 Here is my current set up. 这是我当前的设置。

GameScene.swift contains the game code GameScene.swift包含游戏代码

ViewController contains tracking and banner ads ViewController包含跟踪广告和横幅广告

GameOverViewController displays advert, replay and hopefully the score soon. GameOverViewController会显示广告,重播,并希望很快就会得分。

GameOver name of the segue that links the two views 链接两个视图的segue的GameOver名称

scoreLabelNode.text = "Score \\(score)" displays score on GameScene scoreLabelNode.text = "Score \\(score)"GameScene上显示分数

On my GameOVerViewController I've added a label where I would like to show the score as so... 在我的GameOVerViewController我添加了一个标签,希望以此来显示得分...

@IBOutlet weak var Label: UILabel!

var recevedString: String = ""

    override func viewDidLoad() {
    super.viewDidLoad()

        Label.text = recevedString

On my GameScene I've tried the following function 在我的GameScene我尝试了以下功能

func segue(){

    self.viewController.performSegueWithIdentifier("GameOver", sender: viewController)
    var secondViewController: GameOverViewController = segue.destinationViewController as GameOverViewController

    secondViewController.recevedString = scoreLabelNode.text
}

no this correctly points out the following error that () -> () does not have a member named destinationViewController' 不,这正确地指出以下错误: () -> () does not have a member named destinationViewController'

Would anyone know the correct code to help send the high score data or point me in the right direction if I've gone wrong. 有人会知道正确的代码来帮助发送高分数据,或者在我出错时将我指向正确的方向。

Thank you. 谢谢。


UPDATED CODE 更新的代码

Would anyone know why the score still isn't pulling through to my GameOVerViewController after I've made the changes? 进行更改后,有人知道为什么分数仍不能传递到GameOVerViewController吗? I'm guessing something is missing from didBeginContact to call the prepareForSegue`. 我猜测didBeginContact to call the缺少一些东西didBeginContact to call the prepareForSegue`。 Thank you. 谢谢。

func didBeginContact(contact: SKPhysicsContact) {

    if( moving.speed > 0 ) {

        if((contact.bodyA.categoryBitMask & scoreCategory) == scoreCategory || (contact.bodyB.categoryBitMask & scoreCategory) == scoreCategory){

            score++
            scoreLabelNode.text = "Score \(score)"
        }else {

            moving.speed = 0;

//lots of game over code removed from here

            return segue()
        }

    }

}

func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if(segue.identifier == "GameOver")
    {
        let destVC = (segue.destinationViewController as? GameOverViewController)!
        destVC.recevedString = scoreLabelNode.text
    }
}

func segue(){
    self.viewController.performSegueWithIdentifier("GameOver", sender: self)
}

You need to use prepareForSegue I guess. 我猜你需要使用prepareForSegue Try this : 尝试这个 :

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if(segue.identifier == "GameOver")
        {
            let destVC = (segue.destinationViewController as? GameOverViewController)!
            destVC.recevedString = scoreLabelNode.text
        }
 }

and now your segue() will look like 现在您的segue()看起来像

func segue(){
    self.viewController.performSegueWithIdentifier("GameOver", sender: self)
}

Hope this will help! 希望这会有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM