UIKit& SpriteKit问题:计时器和游戏

时间:2018-02-09 13:13:49

标签: ios uiviewcontroller uilabel

我正在开发一款游戏,我的所有游戏代码都在GameScene.swift文件中(新关卡,游戏结束,得分系统等等)。

我想添加一个计时器的功能,以使游戏更有趣,并在这里问了一个问题,这个问题得到了解答。

对于我的问题,有没有办法在GameScene中为计时器创建UILabel而不是将计时器放在UIViewController文件中?在UIViewConroller中引用GameScene的所有内容都很困难。有什么建议吗?

1 个答案:

答案 0 :(得分:0)

您最好为标签创建自定义类并将计时器逻辑放在其中

class customLb:UILabel
{ 
   var timer:Timer!

   func start()
   { 
       self.timer = Timer.scheduledTimer(timeInterval: 1.2  ,
                                             target: self,
                                             selector: #selector(self.doIt), userInfo: nil,repeats: true)

   }

    func doIt()
    {
       self.text = //change here
    }
    func stop()
    {
       if(self.timer != nil)
       {            
          self.timer.invalidate()

          self.timer = nil
       }
     }
 }