Highlighting a gridsquare [SOLVED]

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Ontogenesis
Prole
Posts: 2
Joined: Thu Oct 17, 2013 8:43 pm

Highlighting a gridsquare [SOLVED]

Post by Ontogenesis »

Hello Lovers.
I'll add here the usual disclaimer that I am brand new to Love, an amateur bra-fumbler with Lua, and a generally poor dressed individual. You'll have to forgive me any obvious errors here.

So I've created a grid, and I want the grid square to be highlighted where ever the mouse is.

So I create some tables to hold grid co-ordinates, 4x16 squares, 64 pixels wide each:

Code: Select all

function love.load()
--Battle grid info
GridInfo = {}
local g = GridInfo
g.rowN = 4
g.squareSize = 64
g.squareN = 16
g.imageY = 384

--Creates battle grid co-ordinate table from GridInfo
Grid = {}
for row = 1, g.rowN do
Grid[row] = {}
	for square = 1, g.squareN do
		Grid[row][square] = {}
		local x = (square-1)*g.squareSize
		local y = g.imageY + ((row-1)*g.squareSize)
		Grid[row][square]["x"] = x
		Grid[row][square]["y"] = y
	end
end
end
Then I create a function called in the right state, called 'MouseOver', which identifies if the mouse is over a grid, and gets the co-ordinates of the square if it is. This I use in love.draw to draw the highlight square - simple enough, at least I thought.

Code: Select all

function MouseOver()
local x, y = love.mouse.getPosition()
for row=1, #Grid do
	for square=1, #Grid[row] do
	local gx, gy = Grid[row][square]["x"], Grid[row][square]["y"]
	local s = GridInfo.squareSize
	if (x >= gx) and (x <= gx+s) and (y >= gy) and (y <= gy+s) then
		highlight.ini = true
		highlight.x = gx
		highlight.y = gy
		--print("Row: " .. row .. "Square: " .. square)
		else
		highlight.ini = false
		end
	end
end
end

function love.draw()
local high = love.graphics.newImage("highlight.png")
if highlight.ini then
love.graphics.draw(high, highlight.x, highlight.y)
end
end

function love.update(dt)

--if game.state == "battle_prep" then
MouseOver()
--end
end
When I include the if statement detecting highlight.ini == true, the only square that is highlighted is the very bottom right hand corner. If I take this condition out, then the highlight squares are drawn normally, i.e. where the mouse is.

So, my question, why is it doing this? highlight.ini seems to remain false in all other squares, even though the squares are successfully being drawn without it (it gets the co-ordinates in needs from the same chunk where highligh.ini is set to true - I've included a commented out line which prints the row and square to demonstrate this). I sense there is a reason that makes sense in here somewhere.

EDIT: Hmm. May have got it - I think it's simply cycling through too fast. I need to re-write it so that it keeps the current grid square, and notices changes instead. Hmmmmm...
Last edited by Ontogenesis on Thu Oct 17, 2013 11:07 pm, edited 1 time in total.
Ontogenesis
Prole
Posts: 2
Joined: Thu Oct 17, 2013 8:43 pm

Re: Highlighting a gridsquare [SOLVED]

Post by Ontogenesis »

Ah solved it. Was a bit stupid and wasn't checking if the square was the same, so it kept returning false. Solved it with an extra elseif (I'll tidy it up later):

Code: Select all

function MouseOver()
local x, y = love.mouse.getPosition()
for row=1, #Grid do
	for square=1, #Grid[row] do
	local pgrid = Grid[row][square]
	local gx, gy = pgrid.x, pgrid.y
	local s = GridInfo.squareSize
	if ((x >= gx) and (x <= gx+s)) and ((y >= gy) and (y <= gy+s)) then
		highlight.ini = true
		highlight.x = gx
		highlight.y = gy
		highlight.current = pgrid
		elseif (x >= highlight.current.x and x <= highlight.current.x+s) and (y >= highlight.current.y and y <= highlight.current.y+s) then
		else
		highlight.ini = false
		end
	end
end
Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests