在Swift中使用Sprite工具包更改场景一段时间后?

我想在Swift的一个场景中,在一定的时间之后,从一个场景转换到另一个场景。 我正在尝试创造一种生存型的游戏,玩家必须在这样一个阶段才能生存下去,然后才能进入下一个阶段。

我知道,在我被调用后,我会用这个去下一个场景。

self.view?.presentScene(GameScene2()) 

我相信NSTimer将会被使用,但是任何可以给我指出正确方向的东西都会有很大的帮助。 先谢谢你。

当你的视图被实例化时,你想要开始一个NSTimer。 请参阅此页面以获取帮助: 如何在Swift中使用NSTimer? 。 这个页面可能也有帮助: 在Swift中使用NSTimer 。

 override func viewDidLoad() { super.viewDidLoad() var timer = NSTimer.scheduledTimerWithTimeInterval(30.0, target: self, selector: "update", userInfo: nil, repeats: true) 

那么,你可以把这个

 self.view?.presentScene(GameScene2()) 

在一个func更新如下所示:

 func update() { self.view?.presentScene(GameScene2()) 

希望这可以帮助。 如果是这样,请检查我的答案。 如果没有,评论,我会尽力帮助。

NSTimer是一种select。 取决于你需要什么样的准确性,你的持续时间将会是多久,你可能会或可能不想使用NSTimer。 NSTimer的例子

 var myTimer = NSTimer() func startMyTimer() { myTimer = NSTimer.scheduledTimerWithTimeInterval(1.5, target: self, selector: "myFunction", userInfo: nil, repeats: true) //1.5 is the time interval that you want to call the timer, in this case every 1.5 seconds //the selector calls myFunction which is the function that you want to call every time the timer reaches its time interval //if you want the timer to repeat and call myFunction() every 1.5 seconds then repeat should be true. if you only want it to be called once then repeat should be false } func myFunction(){ //do whatever i am supposed to do when the timer reaches my specified interval } 

现在这可能不是你正在寻找的解决scheme。 另一种方法是使用GCD(grand Central Dispatch) dispatch_after 。 在这里可以find一个非常好的,非常容易使用的函数, dispatch_after – GCD在swift中? ,在第二个答案下,赞成stackoverflow的@matt