jeudi 7 juillet 2016

Using variables in another class to store integers (Swift in iOS)

I am building a simple tap war game where there are 2 sides of the screen spilt evenly down the middle. It is a 2 player game where the objective is to simply get to 20 taps first. Once a player does score 20 points, the code performs a segue to a restart screen. In this restart screen I want there to be a label where it displays the number of wins each player has. How do I correctly reference the classes and integers in my if statement below to add a win to the winning player? (I just want it to be something like Red: (redWins) Blue: (blueWins))

import UIKit

class endGameFinalScore {

    var ScoreInteger: String = ""
    var redWins: Int = 0
    var blueWins: Int = 0
}

In another view controller I test to see if either player has reached a score of 20. If the blue player scored 20, I want to add 1 to the integer that scores their number of wins.

func testEndGame() {

    if redScoreInt >= 20 {

        endGameFinalScore.redWins += 1
        redScoreInt = 0
        self.performSegueWithIdentifier("endGameSegue", sender: nil)

    }

    if blueScoreInt >= 20 {

        endGameFinalScore.blueWins += 1
        blueScoreInt = 0
        self.performSegueWithIdentifier("endGameSegue", sender: nil)

    }

Aucun commentaire:

Enregistrer un commentaire