获取加速度计输入 - Defold Engine
Getting accelerometer input - Defold Engine
我正在尝试使用加速度计移动角色。
但是在on_input()
中使用action.acc_x/action.acc_y/action.acc_z,我只能在执行其他操作时才能获取信息( "click"、"left"、"right")
我想将脚本更改为仅随着设备的移动来移动角色
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
据我所知,当 action_id
为 nil
时,Defold 只有一种情况 - 即从加速度计接收到数据时。
这意味着你可以写:
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))
我正在尝试使用加速度计移动角色。
但是在on_input()
中使用action.acc_x/action.acc_y/action.acc_z,我只能在执行其他操作时才能获取信息( "click"、"left"、"right")
我想将脚本更改为仅随着设备的移动来移动角色
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
据我所知,当 action_id
为 nil
时,Defold 只有一种情况 - 即从加速度计接收到数据时。
这意味着你可以写:
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))