Create drag and drop games? (SOLVED)

Is there any possible way to Create drag and drop games in Defold like magic triangle?

Do you mean this game?

http://www.mathplayground.com/magic_triangle.html

If so, yes, absolutely. What part is giving you trouble?

well i can’t read the dropped values…

What do you have so far? I’m guessing that you have a number of game objects, each representing a draggable ball? Each game object has a label or a sprite to indicate the number on the ball? Why not map (in a Lua table) the game object id to the assigned number and when you detect that a game object is dragged you can look up the assigned number from said table. Like this (note that code is written on the fly and untested):

function init(self)
	self.balls = {}
	-- iterate over balls and assign a number to each
	-- I'm assuming 6 balls with ids "ball1" to "ball6"
	for i=1,6 do
		local num = get_number_for_ball(i)
		local id = go.get_id("ball" .. i)
		self.balls[id] = num
	end
end


-- I'm assuming that you're doing hit detection using physics
-- I'm assuming that the balls have collision shapes of type trigger
function on_message(self, message_id, message, sender)
	if message_id == hash("trigger_response") then
		if message.enter then
			self.current_ball = message.other_id
		else
			self.current_ball = nil
		end
	end
end

function on_input(self, action_id, action)
	if action_id == hash("touch") and self.current_ball then
		if action.pressed then
			local num = self.balls[self.current_ball]
			self.dragging = true
			print("picked up ball with number" , num)
		elseif action.released then
			local num = self.balls[self.current_ball]
			self.dragging = false
			print("dropped ball with number", num)
		end
	end
	-- update position of ball if we are dragging it
	if self.current_ball and self.dragging then
		go.set_position(vmath.vector3(action.x, action.y, 0), self.current_ball)
	end
end
2 Likes

well they are not balls…im using multiple buttons with random numbers for this game how can i get the that dropped values…?

Circles or squares, shouldn’t be that much different I guess? You are providing very little information about what you have done so I’m doing a lot of guessing here. I’m going to do some more guessing and if I’m wrong then you need to be more explicit and maybe even share some code. OK?

I’m guessing that you are using GUI nodes instead? The same principle should still apply and with only minor modifications you end up with this:

function init(self)
	-- iterate over buttons and assign a number to each
	-- I'm assuming 6 buttons with ids "button1" to "button6"
	-- I'm assuming 6 text fields with ids "text1" to "text6"
	for i=1,6 do
		local button_node = gui.get_node("button" .. i)
		local text_node = gui.get_node("text" .. i)
		local num = get_number_for_ball(i)
		gui.set_text(text_node, tostring(num))
	end
end


function on_input(self, action_id, action)
	if action_id == hash("touch") then
		-- iterate over buttons and find if we're on top of any of them
		for i=1,6 do
			local node = gui.get_node("button" .. i)
			if gui.pick_node(node, action.x, action.y) then
				-- get number on the button we've picked
				-- convert it from a string to a number
				-- detect if picked up or dropped
				local number_on_button = tonumber(gui.get_text(gui.get_node("text" .. i)))
				if action.pressed then
					print("picked up button with number" , num)
					self.current_button = node
					self.dragging = true
				elseif action.released then
					print("dropped button with number" , num)
					self.current_button = nil
					self.dragging = false
				end
				break
			end
		end
	end
	-- update position of button if we are dragging it
	if self.current_button and self.dragging then
		gui.set_position(self.current_button, vmath.vector3(action.x, action.y, 0))
	end
end
4 Likes

yeah this is what i’m looking for thanks britzl…