我正在开发一款游戏,我的所有游戏代码都在GameScene.swift文件中(新关卡,游戏结束,得分系统等等)。
我想添加一个计时器的功能,以使游戏更有趣,并在这里问了一个问题,这个问题得到了解答。
对于我的问题,有没有办法在GameScene中为计时器创建UILabel而不是将计时器放在UIViewController文件中?在UIViewConroller中引用GameScene的所有内容都很困难。有什么建议吗?
答案 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
}
}
}