Problem with Twin Sticks in Defold (SOLVED)


#1

Hello Guys
Just observed a new problem when testing out my game on Android. I have a twin stick interface in my game. But I have observed that when you want to press both of them at the same time, only one of them responds. I tried checking my scripts, but couldn’t find any thing that can cause that problem. For reference here is the script(Just an extension of @britzl’ s multi touch example):

local function handle_touch(self, touch, touch_index)
	
	if touch.pressed then
		if gui.pick_node(self.fire, touch.x, touch.y) then
			gui.cancel_animation(self.fire, gui.PROP_POSITION)
			local touch_pos = vmath.vector3(touch.x, touch.y, 0)
			self.fire_pressed = { pos = touch_pos, offset = touch_pos - self.fire_start_position, index = touch_index }
		elseif gui.pick_node(self.melee, touch.x, touch.y) then
			post_to_listener(self, "melee")
		elseif gui.pick_node(self.reload, touch.x, touch.y) then
			post_to_listener(self, "action", {action = true})
			self.action = true
			print("Action")
		end
		if gui.pick_node(self.analog, touch.x, touch.y) then
			gui.cancel_animation(self.analog, gui.PROP_POSITION)
			local touch_pos = vmath.vector3(touch.x, touch.y, 0)
			self.analog_pressed = { pos = touch_pos, offset = touch_pos - self.analog_start_position, index = touch_index }
		end
	elseif touch.released then
		if self.analog_pressed and self.analog_pressed.index == touch_index then
			gui.animate(self.analog, gui.PROP_POSITION, self.analog_start_position, gui.EASING_OUTQUAD, 0.2)
			post_to_listener(self, "move", { x = 0, y = 0 })
			self.analog_pressed = nil
		elseif self.action then
			post_to_listener(self, "action", {action = false})
			self.action = false
		elseif self.fire_pressed and self.fire_pressed.index == touch_index then
			gui.animate(self.fire, gui.PROP_POSITION, self.fire_start_position, gui.EASING_OUTQUAD, 0.2)
			self.fire_pressed = nil
			post_to_listener(self, "fire", { released = true })
		end
	end
	if self.analog_pressed and self.analog_pressed.index == touch_index then
		local touch_position = vmath.vector3(touch.x, touch.y, 0)
		local diff = self.analog_pressed.pos - touch_position
		local dir = vmath.normalize(diff)
		local distance = vmath.length(diff)
		if distance > 0 then
			local radius = 80
			if distance > radius then
				touch_position = self.analog_start_position - dir * radius
				distance = radius
			else
				touch_position = touch_position - self.analog_pressed.offset	
			end
			gui.set_position(self.analog, touch_position)
			post_to_listener(self, "move", { x = -dir.x * distance / radius, y = -dir.y * distance / radius })
		end
	end
	if self.fire_pressed and self.fire_pressed.index == touch_index then
		local touch_position = vmath.vector3(touch.x, touch.y, 0)
		local diff = self.fire_pressed.pos - touch_position
		local dir = vmath.normalize(diff)
		local distance = vmath.length(diff)
		if distance > 0 then
			local radius = 80
			if distance > radius then
				touch_position = self.fire_start_position - dir * radius
				distance = radius
			else
				touch_position = touch_position - self.fire_pressed.offset
			end
			gui.set_position(self.fire, touch_position)
			local new_angle = math.atan2((touch_position.y - self.fire_start_position.y), (touch_position.x - self.fire_start_position.x))
			post_to_listener(self, "fire", {angle = new_angle, released = false, left = (touch_position.x < self.fire_start_position.x)})
		end
	end
end



function on_input(self, action_id, action)
	input.update(action_id, action)

	local ratio_x = action.x / (action.screen_x or action.x)
	local ratio_y = action.y / (action.screen_y or action.y)
	if action.touch and (not player_data.GAME_PAUSED) then
		for i,tp in pairs(action.touch) do
			handle_touch(self, tp, i)
		end
	elseif not  player_data.GAME_PAUSED then
		handle_touch(self, action, 0)
	end
	
end

#2

Do other multitouch demos work on your test device? Did you test the multitouch example unmodified on your device?


#3

Yes, it works, But then what am I doing wrong, that it refuses to work in my game(It is more or less the same code)?
Here is my entire HUD script, just in case:
hud.script (16.1 KB)
Danger:- It is a gui script, just saved as .script to upload here.


#4

Solved
Actually I forgot to add multi touch to my input bindings(so strange, isn’t it)