Getting accelerometer input - Defold Engine

107 views Asked by At

I'm trying to use the accelerometer to move the character.

But with the use of action.acc_x / action.acc_y / action.acc_z inside on_input(), I can only get the information when there is also some other action being executed ("click", "left", "right")

I would like to change the script to only move the character with the movement of the device

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: game.script

1

There are 1 answers

0
Alexey Gulev On BEST ANSWER

As I know Defold has only one situation when action_id is nil - it is when data received from the accelerometer. That mean you can write:

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

Do not forgot to replace

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

with

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