1

我正在尝试使用加速度计来移动角色。

但是使用 action.acc_x / action.acc_y / action.acc_z inside on_input(),我只能在还有其他一些动作正在执行时才能获得信息(“点击”,“左”,“右”)

我想将脚本更改为仅随着设备的移动而移动角色

function on_input(self, action_id, action)

    label.set_text("info#action", "action: ".. action_id)
    label.set_text("info#info", "info: ".. tostring(action.acc_x) ..", ".. tostring(action.acc_y) ..", "..tostring(action.acc_z))
    if action.acc_x > 0 then
        self.player_direction.x = 1
    elseif action.acc_x < 0 then
        self.player_direction.x = -1
    else
        self.player_direction.x = 0
    end
end

github:游戏.脚本

4

1 回答 1

4

据我所知,Defold 只有一种情况——action_idnil从加速度计接收数据时。这意味着你可以写:

function on_input(self, action_id, action)
  if not action_id then
    label.set_text("info#action", "action: ".. tostring(action_id))
   label.set_text("info#info", "info: ".. tostring(action.acc_x) ..", ".. tostring(action.acc_y) ..", "..tostring(action.acc_z))
    if action.acc_x > 0 then
      self.player_direction.x = 1
    elseif action.acc_x < 0 then
      self.player_direction.x = -1
    else
      self.player_direction.x = 0
    end
  end
end

不要忘记更换

label.set_text("info#action", "action: "..action_id)

label.set_text("info#action", "action: ".. tostring(action_id))

于 2017-08-03T13:36:41.930 回答