繁体   English   中英

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

[英]UIKit & SpriteKit Problems: Timer and Games

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

我想添加计时器的功能以使游戏更加有趣,并在此处提出了一个问题,并得到了回答。

对于我的问题,有没有一种方法可以在GameScene中为计时器创建UILabel,而不是将计时器放入UIViewController文件中? 很难引用UIViewConroller中GameScene的所有内容..有什么建议吗?

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

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
       }
     }
 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM