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()
print("Going to sul")
let skView = self.view as! SKView
scene = GameScene(size: skView.bounds.size)
scene.scaleMode = .aspectFill
scene.gameViewController = self
level = Level(filename: "Level_1", scene: scene)
scene.level = level
level.viewController = self
scene.addTiles()
updateLabels()
skView.presentScene(scene)
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()
gameViewController = self
print("Going to vdl")
let skView = self.view as! SKView
skView.isMultipleTouchEnabled = false
scene = GameScene(size: skView.bounds.size)
scene.scaleMode = .aspectFill
scene.gameViewController = self
level = Level(filename: "Level_1", scene: scene)
scene.level = level
level.viewController = self
scene.addTiles()
updateLabels()
skView.presentScene(scene)
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