Highlighting a gridsquare [SOLVED]
Posted: Thu Oct 17, 2013 9:03 pm
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:
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.
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...
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
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
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...