Tuesday, September 19, 2023
HomeiOS Developmentios - Swift: playAgainstComputer-variable adjustments from true to false after I navigate...

ios – Swift: playAgainstComputer-variable adjustments from true to false after I navigate between my viewControllers


I’m engaged on a swift associated iOS-app in Xcode the place I’ve two viewControllers: SecondViewController and GameViewController. I’m utilizing a boolean variable, playAgainstComputer, to determine if the sport needs to be towards a pc or one other participant. I’ve observed that after I set playAgainstComputer to true in SecondViewController and navigate to GameViewControler, the worth of playAgainstComputer robotically adjustments to false.

Right here is my code the place I set playAgainstComputer to true after person press on button “Or play towards laptop” in SecondViewController:

@IBAction func btnPlayNPC(_ sender: UIButton) {
       playAgainstComputer = true
        print("playAgainstComputer: (playAgainstComputer)")
        performSegue(withIdentifier: TO_GAME_SCREEN, sender: self)
    }

And right here is the code in my GameViewController the place I discover that playAgainstComputer adjustments to false:

 @IBAction func buttonTapped(_ sender: UIButton) {
        
        let index = sender.tag
        if boardArray[index] == "" {
            
            if currentPlayer == 1 {
                boardArray[index] = "X"
                
            } else {
                boardArray[index] = "O"
            }
            
            sender.setTitle(boardArray[index], for: .regular)
            
            switchPlayers()
            
            if currentPlayer == 1 {
                turnSign.textual content = playerOneName
            } else {
                turnSign.textual content = playerTwoName
            }
            
            if let winnerMessage = checkForWinner() {
                self.winner = winnerMessage
                myAlertMessage(winnerMessage: winnerMessage)
            } else if !boardArray.incorporates("") {
                myAlertMessage(winnerMessage: "It is a tie!")
            }
            print("playAgainstComputer: (playAgainstComputer), currentPlayer: (currentPlayer)")
            if playAgainstComputer && currentPlayer == 2 {
                print("HERE")
                playAgainstNPC()
                makeComputerMove()
            }
        }
    }

I do not know if this half is related however whether it is then right here is my code the place I create my variable in a swift file:

import Basis

class GameSettings {
    static let shared = GameSettings()
    var playAgainstComputer = false
}

Initially I’ve double-checked that I’m utilizing the identical occasion of GameSettings in each SecondViewController and GameViewController to make sure that the playAgainstComputer variable is about the identical means in each view controllers.
Then I’ve used print statements to trace the worth of the playAgainstComputer variable when urgent the “Or play towards laptop” button in SecondViewController. I’ve confirmed that playAgainstComputer is about to true when the button is pressed, which is the specified habits.
I’ve seemed for any code sections in GameViewController that might doubtlessly change the worth of playAgainstComputer from true to false, however I’m not sure that I’ve discovered any such sections.
I even have tried to make use of the playAgainstComputer variable in GameViewController, however I discover that the worth adjustments from true to false when navigating from SecondViewController to GameViewController. This ends in my laptop participant not functioning as anticipated.



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments