施加力量 速度越来越快

嘿,我的问题是,如果我点击并按住屏幕,则玩家会在Y轴上加速。

我希望玩家点击屏幕(并保持浮动向上),他以稳定的速度上升(不会变快)。

这是用于浮动速度和触摸事件的函数:

function activateJets(ship,event)
    ship:applyForce(0, -1.0, ship.x, ship.y)
    print("run")
end

function touchScreen(event)
    print("touch")
    if event.phase == "began" then
        ship.enterFrame = activateJets
        Runtime:addEventListener("enterFrame", ship)
    end
    if event.phase == "ended" then
        Runtime:removeEventListener("enterFrame", ship)
   end
end

Runtime:addEventListener("touch", touchScreen)

如果这不太合理,请谅解。以下是我想要的大致想法:

  • 玩家触摸屏幕(并保持浮动)
  • 然后对象以恒定速度浮动(不会提高速度)
  • 玩家释放触摸
  • 对象正常降落
点赞
用户2633423
用户2633423

物理学的原理使得你无法做到这一点:你正对你的飞船施加一个恒定的力。根据牛顿定律(由物理库模拟),这意味着一个恒定的加速度,因此速度线性增加。

你想要的行为(与真实物理不一致)是瞬间加速到目标速度,然后没有速度变化。因此,只需在你的 activateJets 函数中使用 [shipSetLinearVelocity()](http://docs.coronalabs.com/api/type/Body/setLinearVelocity.html) 将飞船的速度设置为常数即可。当触摸结束时,当然应该将速度重置为零。

2013-10-13 19:31:28