如何在 LUA、2D 中实现枪的后坐力?

How do I implement recoil on a gun in LUA, 2D?

我正在尝试创建一个枪支模拟器类型的移动应用程序,目前正在尝试编写一个开火功能。到目前为止一切顺利,但作为新手,我很难弄清楚我能做些什么来解决我的问题:

function pistolRecoil()
transition.to(pistol, {time = 30, rotation = -20 })
end

function revPistolRecoil()
transition.to(pistol, {time = 50, rotation = 20 })
end

--Fire the pistol--
function tapFirePistol (event)
--display flash
muzzleFlashP:toFront()
timer.performWithDelay(20, muzzleFlashPFunc())
pistolAmmo = pistolAmmo - 1
----timings
--sound(.wav),
audio.play(pFireSound)
--rotate,rotate
pistolRecoil()
print("1st", pistol.rotation)
pistolRecoil()
print("2nd", pistol.rotation)
revPistolRecoil()
----timings
if pistolAmmo <= 0 then fireButtonPistol:removeEventListener("tap", 
tapFirePistol)
pistolAmmoCount.text = pistolAmmo
end
if pistolAmmo <= 0 then pistolAmmo = 0
pistolAmmoCount.text = pistolAmmo
end
pistolAmmoCount.text = pistolAmmo
end

fireButtonPistol:addEventListener("tap", tapFirePistol)

我遇到的问题是枪会后坐,但会保持旋转,这很糟糕,因为我希望它在短时间后 "fall" 后退,并且不会任何后坐更进一步,即使在手枪仍在旋转的情况下点击开火按钮(我不介意)

非常感谢任何帮助。

因此,后坐力方面现在可以正常使用,因为我现在正在使用 timer.performWithDelay。我发现 timer.performWithDelay 没有按我预期的方式运行的原因是因为我在 performWithDelay 括号内使用了不需要的括号,而且我还指定它应该完成一次。

timer.performWithDelay(20, revPistolRecoil, 1)

我仍然调用 pistolRecoil 作为函数。

附带说明一下,当我应用相同的更改时,muzzleFlashP 现在也可以工作了。

感谢您的帮助!