I have a timer in my game scene that has keeps track of a runCount variable. Within the didMove function I have an if statement that says if the runCount == 5 then there will be another function that creates an obstacle and resets the timer. This function work when its outside of didMove but when it's there, the obstacle cannot be called because it's inside didMove. I've tried globalizing the variable and it is still not recognized.
class GameScene: SKScene {
var runCount = 0
@objc func globalTimer(timer: Timer) -> Int {
runCount += 1
print(runCount)
return(runCount)}
override func didMove(to view: SKView){
func runObstacle() {
//code
}
if runCount == 5 {
runObstacle()
runCount = 0
}
}
let timer = Timer(fireAt: date, interval:1, target: self, selector:#selector(globalTimer(timer:)), userInfor: nil, repeats: true)
RunLoop.main.add(timer, forMode: .common)
The code inside the if statement works fine when its not in the didMove function but then it cannot call all the elements of the obstacle. How can I have both?
Aucun commentaire:
Enregistrer un commentaire