Only one input per time

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

You could make use of the ‘action.pressed’ and ‘action.released’ input controls to set a true/false variable when moving or not.

Don’t do the actual movement on_input, do it in a function you call on_update to check if any movement flags are enabled from on_input, then at the end of on_update you disable all of the flags (true/false bools).

2 Likes

thanks for helping

2 Likes

i will try this, thanks for helping :smiley:

1 Like