I’m creating a system for moving in a grid, and this is working perfectly, but I wanted to know how I can check one input at a time, at the moment if I perform the action to move up and to the side while he walks diagonally, I wanted only one action to be possible, to move in 4 directions, I tried to use elseif but it did not work
function init(self)
msg.post(".", "acquire_input_focus")
player_pos = go.get_position()
local cellsize = 8
grid_pos = vmath.vector3(player_pos.x/cellsize, player_pos.y/cellsize, player_pos.z)
end
function final(self)
-- Add finalization code here
-- Remove this function if not needed
end
function update(self, dt)
-- Add update code here
-- Remove this function if not needed
end
function on_message(self, message_id, message, sender)
-- Add message-handling code here
-- Remove this function if not needed
end
function on_input(self, action_id, action)
print(action_id)
if action_id == hash("up") then
if check_move(grid_pos.x, grid_pos.y+1,"layer0") ~= 244 then
msg.post(".", "release_input_focus")
go.animate(".", "position.y", go.PLAYBACK_ONCE_FORWARD,
player_pos.y+16 , go.EASING_LINEAR, 0.3, 0, move_end)
player_pos.y = player_pos.y +16
grid_pos.y = grid_pos.y+1
end
elseif action_id == hash("down") then
if check_move(grid_pos.x, grid_pos.y-1,"layer0") ~= 244 then
msg.post(".", "release_input_focus")
go.animate(".", "position.y", go.PLAYBACK_ONCE_FORWARD,
player_pos.y-16 , go.EASING_LINEAR, 0.3, 0, move_end)
player_pos.y = player_pos.y -16
grid_pos.y = grid_pos.y-1
end
elseif action_id == hash("right") then
if check_move(grid_pos.x+1, grid_pos.y,"layer0") ~= 244 then
msg.post(".", "release_input_focus")
go.animate(".", "position.x", go.PLAYBACK_ONCE_FORWARD,
player_pos.x+16 , go.EASING_LINEAR, 0.3, 0, move_end)
player_pos.x = player_pos.x+16
grid_pos.x = grid_pos.x+1
end
elseif action_id == hash("left") then
if check_move(grid_pos.x-1, grid_pos.y,"layer0") ~= 244 then
msg.post(".", "release_input_focus")
go.animate(".", "position.x", go.PLAYBACK_ONCE_FORWARD,
player_pos.x-16 , go.EASING_LINEAR, 0.3, 0, move_end)
player_pos.x = player_pos.x-16
grid_pos.x = grid_pos.x-1
end
end
end
function on_reload(self)
-- Add reload-handling code here
-- Remove this function if not needed
end
function check_move(x,y,layer)
local tile = tilemap.get_tile("/mapa#Mapa 1", hash(layer), x, y)
return tile
end
function move_end()
msg.post(".", "acquire_input_focus")
end