Hi, I’m new here, I’m studying Defold for mobile game development.
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
Hmm that seems strange, I verified this on an iOS device and get inputs from accelerometer every frame. What does your input bindings look like? What device are you using?
EDIT
I suspect your issue is caused by action_id being nil on the device when the action contains no touch/mouse input, but only accelerometer data in your case. The script will probably print an error and stop execution when you do
It is strange that this acc_ parameter has value with other actions.
I think now you can add next checking:
if not action_id then…
Something like:
function on_input(self, action_id, action)
if not action_id then
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
end
As I understood problem is about other (“click”, “tap” and so on) actions have the action.acc_x, action.acc_y, action.acc_z parameters and has no action_id.
As I know defold has only one situation when action_id is nil - it is when data received from the accelerometer.
But yes, original code will be fail when some of parameters will be nill.
As option you can replace all concatenate sybbols .. (double dot) wit , (comma) (it works only with print function)
UPD:
It is strange that action_id is nil, maybe have sense to make it something like accelerometer
And one more strange that developer can’t turn off accelerometer, really old task DEF-1396