Re: How can i divide up a map?
Posted: Wed Feb 08, 2012 2:10 pm
Thank you both of you.
Code: Select all
-- boot.lua (I think that is what it is called)
if event == "mousepressed" then
local x, y = love.mouse.getPosition()
local b = -- get button pressed
love.mousepressed(x, y, b)
end
-- main.lua
function love.mousepressed(x, y b)
-- The x, y, and b are already the correct mouse x, y coordinates, and the correct button pressed.
-- Simply use them.
end
This was true.MarekkPie wrote:It looked like you were a bit confused as well on how love.mousepressed() (or for that matter any of the LOVE callbacks work).
Thank you! All good here now.MarekkPie wrote:Change "b" to something else. It seems there is a name conflict with some parameter (most likely the "b" in love.mousepressed(x,y,b)), which nil's out the b variable by the time it reaches love.draw(). Here is it working.
Code: Select all
for k,v in pairs(colors) do
if r == v.r and g == v.g and pb == v.b and v == red then
love.graphics.print("Oklahoma.",0,0)
end
if r == v.r and g == v.g and pb == v.b and v == green then
love.graphics.print("Texas.",0,0)
end
end
Code: Select all
function state(r, g, b, callback)
local s = {
r = r,
g = g,
b = b,
callback = callback,
}
return s
end
function love.load()
colorMap = love.image.newImageData(--[[Path to colormap]])
states = {
Texas = state(100, 100, 100, function() --[[Do something]] end),
Oklahoma = state(200, 200, 200, function() --[[Do something else]] end),
...
}
mouse = {}
end
function love.update(dt)
if next(mouse) ~= nil then -- checks to see if we have clicked once
for _,v in pairs(states) do
if mouse.r == v.r and mouse.g == v.g and mouse.b == v.b then
v.callback() -- This will perform the function you gave to the state
end
end
end
end
function love.mousepressed(x, y)
mouse.r, mouse.g, mouse.b = colorMap:getPixel(x, y)
end
Thanks. I only understand when i have to figure it out by myself or it just gets too easy...MarekkPie wrote:You'll want to treat each state as if it were a button; however, this button doesn't care about it's position, it just cares about its color and its callback. I don't know if you've every done any object-oriented programming, but read up on it if you haven't.
Here is a very simple example (I've explicitly made it so it won't work "perfectly" in your code, so hopefully you can get a better understanding of everything by figuring it out yourself):Code: Select all
function state(r, g, b, callback) local s = { r = r, g = g, b = b, callback = callback, } return s end function love.load() colorMap = love.image.newImageData(--[[Path to colormap]]) states = { Texas = state(100, 100, 100, function() --[[Do something]] end), Oklahoma = state(200, 200, 200, function() --[[Do something else]] end), ... } mouse = {} end function love.update(dt) if next(mouse) ~= nil then -- checks to see if we have clicked once for _,v in pairs(states) do if mouse.r == v.r and mouse.g == v.g and mouse.b == v.b then v.callback() -- This will perform the function you gave to the state end end end end function love.mousepressed(x, y) mouse.r, mouse.g, mouse.b = colorMap:getPixel(x, y) end