问题描述
如果有人熟悉Lua上的knife.timer,请问一下我的代码并告诉我我有什么问题吗?
我希望做两件事:
使用以下代码,我的倒数计时器从9开始,但正好到负数十。我的角色在感觉到4秒钟后开始闪烁,并在更改状态后继续闪烁几秒钟。
我主要是Timer:update(dt)
,所以我不确定为什么时间不对。而且我认为,完成角色的16次闪烁之前,完成不会调用更改状态函数。
function PlayerPilotState:update(dt)
self.player.currentAnimation:update(dt)
Timer.every(1,function()
self.timer = self.timer - 1
end)
Timer.after(6,function()
Timer.every(0.2,function()
self.player.blinking = not self.player.blinking
self.player.otherPlayer.blinking = not self.player.otherPlayer.blinking
end):finish(function()
self.player:changeState('falling')
self.player.otherPlayer:changeState('falling')
end):limit(16)
end)
end
谢谢!
解决方法
- 使用基本的倒数计时器,您无需指定何时停止。尝试使用
:limit(9)
或self.timer = math.max(0,self.timer - 1)
2。由于您使用了Timer.after,因此您是否已正确计时(很难感觉到已经过了多少时间)。 :finish()
函数在:after()
内部和:every()
之后发生,这可能会使事情变得怪异。我建议在:limit
之前添加:finish()
。
Timer.after(6,function()
Timer.every(0.2,function()
self.player.blinking = not self.player.blinking
self.player.otherPlayer.blinking = not self.player.otherPlayer.blinking
end):limit(16):finish(function()
self.player:changeState('falling')
self.player.otherPlayer:changeState('falling')
end)
end)
,
以上两个评论都非常有用。事实证明,对计时器进行更新不是一个好主意,因为它们正在调用新计时器或奇怪地刷新。我最终得到的并且效果很好的是:
function PlayerPilotState:enter(params)
Timer.every(1,function()
self.timer = self.timer - 1
end)
Timer.after(6,function()
self.player.blinking = not self.player.blinking
self.player.otherPlayer.blinking = not self.player.otherPlayer.blinking
end)
:limit(15)
:finish(function()
self.player.blinking = false
self.player.otherPlayer.blinking = false
self.player:changeState('falling')
self.player.otherPlayer:changeState('falling')
end)
end)
end
我会有一个后续问题,是如何在不使用enter函数的情况下做到这一点?我猜想任何仅调用一次的函数调用(可能带有布尔标志即可调用)都可以。谢谢!