New here,what am i doing wrong?

I want to make a match 3 game(focusing on android)
i used the match 3 template from defold and on the board script i was doing this

this so it remembers the first and second touchs

function on_input(self, action_id, action)
	if action_id == hash("touch") and action.value == 1 then
		--
		-- What block was touched or dragged over?
		--
		local x = math.floor((action.x - edge) / blocksize)
		local y = math.floor((action.y - bottom_edge) / blocksize)
		
		if x < 0 or x >= boardwidth or y < 0 or y >= boardheight or self.board[x][y] == nil then
			--
			-- Input is outside board
			--
			return
		end
		
		if action.pressed then
			--
			-- Mark the first slot
			--
			self.mark_1 = self.board[x][y]
			msg.post(self.mark_1.id, "zoom_and_wobble")
			self.dragging = true
		elseif self.dragging and self.mark_2 == nil then
			--
			-- Mark fish 2 if it's adjacent to mark 1
			--
			local beside = math.abs(self.mark_1.x - x)
			local above = math.abs(self.mark_1.y - y)
			
			if beside == 1 and above == 0 or above == 1 and beside == 0 then
				self.mark_2 = self.board[x][y]
				msg.post(self.mark_2.id, "zoom_and_wobble")
			end
		end

and here i want to check if there are neighbors for the first mark on the place where the mark2 is,then swap,if there isn’t swap and swap back

-- If there are two marked slots, initiate a swap!

for fish in iterate_fishes(board) do
		if self.mark_2.neighbors_horisontal >= 2 or  self.mark_1.neighbors_horisontal >= 2 or  self.mark_1.neighbors_vertical >= 2 or  self.mark_2.neighbors_vertical >= 2 then
		
	
			msg.post("#", "swap", { slot1 = { x = self.mark_1.x, y = self.mark_1.y }, slot2 = { x = self.mark_2.x, y = self.mark_2.y } })
			else
			msg.post("#", "swap", { slot1 = { x = self.mark_1.x, y = self.mark_1.y }, slot2 = { x = self.mark_2.x, y = self.mark_2.y } })
			msg.post("#", "swap", { slot1 = { x = self.mark_1.x, y = self.mark_1.y }, slot2 = { x = self.mark_2.x, y = self.mark_2.y } })
			end
			
		
			--
			-- Nil so we can mark again
			--
			self.mark_1 = nil
			self.mark_2 = nil
		end		
	end
end

it marks them but it won’t swap,what am i doing wrong?

Can you post your iterate_fishes function as well?

In for fish in iterate_fishes(board) do you are not using the fish variable, you might need to replace self in that block with fish, otherwise it’s running the same logic for each iteration.

It’s kind of hard to tell though without seeing the full code.

2 Likes