Android input controls (SOLVED)

I am working on a platformer, so i want the four basics of left,right up and down on the left of the screen. Also, i want two buttons on the right.

I saw that are a lot of examples using mouse and keys triggers, but couldn’t find any using touch triggers.

*Can you point me to any example that uses touch triggers?
*When testing touch triggers, is it necessary to deploy on mobile first, or can we test the trigger by click on mouse for example?

When you configure left mouse button it doubles as single touch on mobile.

Have a look at this multi-touch example for virtual touch pad input: https://github.com/britzl/publicexamples/tree/master/examples/virtual_gamepad

I tried to do something simple, so here is what i did. Of course my solution is simplified version of what @britzl provided me. The idea is to setup a mouse click input, and then make sure that a touch/click have occurred at that position.

Here is what i did:

  • I have created an input of Mouse_Button_1 with action “touch”
  • I created a three files:
    - “gui.atlas”. This file contains all the assets need for the android controls.
    - “globalgui.gui”. This is my gui object. Inside, i have created four nodes. A node for each control movement. Also, i have created a texture which was mapped to the atlas. Each node created has the Texture property which should be be mapped to its target asset in the texture-atlas.
    - Don’t forget that you need to position the nodes correctly.
    - “globalgi.gui_script”. This is the gui script which communicates between player script and the gui nodes.

This is the my gui script.:

function init(self)
	msg.post(".", "acquire_input_focus")
end

function on_message(self, message_id, message, sender)
    -- check if we received a contact point message
    if message_id == hash("register") then
    	self.listener = sender
    end
end

local function post_to_listener(self, message_id, message)
	if self.listener then
		msg.post(self.listener, message_id, message or {})
	end
end

function final(self)
	msg.post(".", "release_input_focus")
end

function on_input(self, action_id, action)
	local id = nil
	if gui.pick_node(gui.get_node("box_up"), action.x, action.y) then
		id = hash("up")
	elseif gui.pick_node(gui.get_node("box_down"), action.x, action.y) then
		id = hash("down")
	elseif gui.pick_node(gui.get_node("box_left"), action.x, action.y) then
		id = hash("left")
	elseif gui.pick_node(gui.get_node("box_right"), action.x, action.y) then
		id = hash("right")
	end
	
	post_to_listener(self, hash("touch"), { actionid = id, actionobj = action })
end

In the player script, i detect the touch event in the on_message function:

if message_id == hash("touch") then
    	handle_input(self, message.actionid, message.actionobj)
    end

Thank you very much for you support.

1 Like