Multiple inputs (mobile)

The best way to learn is to keep doing mistakes, try to fix them yourself and ask for help if you’re stuck.

1 Like

Alright, will do.
One last quick question…
Is it possible to make it so that you can click and hold button_a and it continuously sends a message rather than one single message being sent for every click? My goal is to make it so that the player can press and hold the button so the character moves left, and the same for the right side. Jumping is achieved by tapping a jump button.

Much thanks!
V

No you don’t want that. You should keep track of the state changes instead (unpressed->pressed and pressed->released). While the state is “pressed” you do whatever action you have assigned to your button.

Ok, how would I look for the “pressed” state?

Right now I have it set up as:
09%20PM

The message contains a pressed or released Boolean value you can check:

Sorry, I’m still not quite sure how I would code the platypus script to receive this command and continuously run self.platypus.right or self.platypus.left, while the node is being clicked and held, and stopped running it when the node was released. So sorry for my not understanding.

When you press a key on your keyboard or when you touch the screen you will get an input event. The event tells you which button it was or if it was a finger on the screen. You also get information if the button/finger pressed or released. Learn more in the manual: https://defold.com/manuals/input/

You need to store this information in a Lua table and then use this data to decide how to move your player. Example:

local LEFT = hash("left") -- action set in a game input binding
local RIGHT = hash("right") -- action set in a game input binding

function init(self)
	-- the Lua table where we store input state keyed on action
	self.input_state = {}
end

function on_input(self, action_id, action)
	if action_id then
		-- for any action we receive we store the action and the pressed/released state
		if action.pressed then
			self.input_state[action_id] = true
		elseif action.released then
			self.input_state[action_id] = false
		end
	end
end

function update(self, dt)
	if self.input_state[LEFT] then
		print("left is pressed!")
	end
end

The buttons on the screen should be treated just like the keys on a keyboard. You store their state and make decisions based on if they are pressed or not.

I’ve actually updated the Platypus example to also include on screen buttons:

https://britzl.github.io/Platypus/

1 Like