Click here to Skip to main content
15,881,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey there.

I'm a newbie to Swift and I'm looking for some help. Please forgive any rookie mistakes.

I'm currently running an app that uses the following update method to move a piece on the board that starts moving from the beginning of the game:

 override func update(_ currentTime: TimeInterval) {
    if (currTime == nil) { currTime = Date() }
    if (NSDate().timeIntervalSince(currTime) > 0.5) {
        shuffle()
        currTime = Date()
    }
}


As a note the func update(_ currentTime: TimeInterval)is called automatically as long as the scene has not been paused and has been presented.

After a player triggers a "GameOver" they are moved by segue to a GameOverViewController. Here, a popUp is shown with a restart game button. I use a global variable to reference back to the original GameViewController from GameOverViewController:

var gameViewController: GameViewController = GameViewController()
When refreshing the game I call the following method:

func setUpLevel() {
    super.viewDidLoad()

    // Configure the view.
    print("Going to sul")
    let skView = self.view as! SKView

    // Create and configure the scene.
    scene = GameScene(size: skView.bounds.size)
    scene.scaleMode = .aspectFill

    scene.gameViewController = self

    // Load the level.
    level = Level(filename: "Level_1", scene: scene)
    scene.level = level
    level.viewController = self

    scene.addTiles()

    //Sync the starting score with the game.
    updateLabels()

    // Present the scene.
    skView.presentScene(scene)

    // Start the game.
    beginGame()
}


The problem is, when I transition back to the GameViewController my update method is no longer running, though I am able to control my piece.

What could I do to "restart" my update method so that it is running when the game is refreshed?

Does it have something to do with how I created a new viewController object?

Here is my gameOver() method that causes a transition to the GameOverViewController popUp.

 func gameOver() {
    let gameOverVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "gameOverID") as! GameOverViewController
    self.addChildViewController(gameOverVC)
    gameOverVC.view.frame = self.view.frame
    self.view.addSubview(gameOverVC.view)
    gameOverVC.didMove(toParentViewController: self)
}


Here is my viewDidLoad in GameViewController for reference:

override func viewDidLoad() {
    super.viewDidLoad()

    //set global variable to proper object
   gameViewController = self

    // Configure the view.
    print("Going to vdl")
    let skView = self.view as! SKView
    skView.isMultipleTouchEnabled = false

    // Create and configure the scene.
    scene = GameScene(size: skView.bounds.size)
    scene.scaleMode = .aspectFill

    scene.gameViewController = self

    // Load the level.
    level = Level(filename: "Level_1", scene: scene)
    scene.level = level
    level.viewController = self

    scene.addTiles()

    //Sync the starting score with the game.
    updateLabels()

    // Present the scene.
    skView.presentScene(scene)

    // Start the game.
    beginGame()
}


In all, I am looking for a way to update the scene so that the when the segue is made back to the game, it is running.

Thank you for your time.

What I have tried:

Using the viewWillAppear method, moving reference, checking progression to make sure nothing is nil
Posted
Updated 6-Feb-18 19:35pm

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900