简体   繁体   中英

Swift - passing game score to second view controller

I'm trying to pass the highscore from my GameScene to my GameOverViewController . I've successfully added the segue GameOver and the app works as expected.

The point of the GameOVerViewController is to display the score, advert and replay option.

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

ViewController contains tracking and banner ads

GameOverViewController displays advert, replay and hopefully the score soon.

GameOver name of the segue that links the two views

scoreLabelNode.text = "Score \\(score)" displays score on GameScene

On my GameOVerViewController I've added a label where I would like to show the score as so...

@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

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'

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? I'm guessing something is missing from 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. 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

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

Hope this will help!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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