Lua代码更新玩家的位置和屏幕


function update(event) -- check if the game is over
  if gameOver == true then
    return
  end
  fWait = (wTime > 0)
  if fWait then
    wTime = wTime – 0.01
    if wTime < 0 then
      wTime = 0
    end
    return
  end
  if hasAccel==true then
    local gx, gy = getAcceleration()
    fx = gx * filter + fx * (1-filter)
    fy = gy * filter + fy * (1-filter)
    updatePlayer(fx, fy)
  end
end
stage:addEventListener(Event.ENTER FRAME, update)

在上面的代码中,fx和fy是什么?为什么gx乘以一个常数而fx乘以一个常数?

如果没有更多的信息,这有点难说,但是看看这个:

local gx, gy = getAcceleration()
fx = gx * filter + fx * (1-filter)

这将获得设备的加速度。基于过滤器没有物理单位的事实,fx也是一种加速(尽管有时开发者会通过假设时间周期等方式走捷径并放弃单位,但我不认为这里是这种情况)。

  • 如果过滤器为0,新的fx只是以前的fx,每次这个update(event)被调用(没有关于fx是如何计算的信息,它可能在程序的其他部分也得到更新)。
  • 如果filter为1,fx仅为gx(设备加速),而不考虑先前的fx值。

所以看起来filter是给设备加速的相对权重:当权重为零时,不使用设备加速,所以fx只是到目前为止计算的东西;当权重为1时,fx完全是由于设备加速,所以它只是gx。对于所有其他中间值,更新的fx是重力和先前fx值的比例混合:filter=0.25意味着使用1/4的设备加速效果,其余的(1-1/4或3/4)fx到目前为止计算。

最新更新