Tile puzzle based on 15 puzzle game tutorial (SOLVED)

Hi! This is my first time developing a game and I’m doing a tile puzzle based on 15 puzzle game tutorial from Defold.
My game is different because the original code is for a puzzle 100% width and height on the display and as you can see my board is only 300x300 and display values are 412x848. In the original code, on the input function the x and y value is width and height / 75. I think it doesn’t work because I didn’t define the position of my board but I don’t know how.
How can I set the position of my board in this case?

function on_input(self, action_id, action)
	
	if action_id == hash("press") and action.pressed and not self.done then
		print(action.x, action.y)

		--Fuera el tablero:
		if action.x < 57 + 1 or action.x > 357 - 1 or action.y < 148 + 1 or action.y > 448 + 1 then
			print("click outside")
			return
		--Dentro del tablero
	elseif action.x >= 57 or action.x <= 357 or action.y >= 148 or action.y > 448 then
			print("click inside")
		end

		if M.pausedGame == true then
			M.mov = M.mov + 0
			msg.post("/gui#exitPanel", "update_mov")
		elseif M.pausedGame == false then
			M.mov = M.mov + 1
			msg.post("/gui#exitPanel", "update_mov")
		else 
			M.mov = M.mov + 1
			msg.post("/gui#exitPanel", "update_mov")
		end
		
		local x = math.ceil(action.x / 75)
		local y = math.ceil(action.y / 75)
		local ex, ey = find(self.board, 0)
		
		if math.abs(x - ex) + math.abs(y - ey) == 1 then
			print("moving tile")
			self.board = swap(self.board, (4-ey)*4+ex, (4-y)*4+x)
			draw(self.board)
			M.mov = M.mov + 1
			msg.post("/gui#exitPanel", "update_mov")
		end
		
		ex, ey = find(self.board,0)
		if inversions(self.board) == 0 and ex == 4 then
			self.done = true
			msg.post("#done", "enable")
			dtimer.stop(node_timer_1)
		end
		
	end
end
3 Likes

No one answer but I let the solution here if anyone is trying to do the same tutorial.
The code init the puzzle on 0,0 position. So if your puzzle is not on that position u have to substract x and y space and then divide it by the number of tiles you want. My image is 300x300 so 75 x 4 = 300.

local x = math.ceil((action.x -57) / 75)
local y = math.ceil((action.y -148) / 75)
6 Likes

Nice! Thank you very much for sharing your solution.

4 Likes

Sharing is caring :love_you_gesture:

5 Likes

Ups, I just wanted to look at the flagged post and unintentionally hit the like button - is there a way to remove this mishap?
Edit: flagged posts gone and with it the like :smiley: