Page 2 of 2

Re: How can i divide up a map?

Posted: Wed Feb 08, 2012 2:10 pm
by legendman3
Thank you both of you.

Re: How can i divide up a map?

Posted: Wed Feb 08, 2012 2:53 pm
by MarekkPie
It looked like you were a bit confused as well on how love.mousepressed() (or for that matter any of the LOVE callbacks work). Callbacks are normal functions, except you are providing the functionality to a pre-built function, as opposed to building it from the ground up.

There is another layer in LOVE that works behind the scenes to make the game initialization much easier. Inside that layer, whenever a mouse press is registered, it gathers the mouse position and the button pressed, and then sends them into the love.mousepressed(x,y,b) for you to manipulate. Here's a general example:

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

Re: How can i divide up a map?

Posted: Wed Feb 08, 2012 9:21 pm
by legendman3
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).
This was true.

Re: How can i divide up a map?

Posted: Thu Feb 09, 2012 12:07 am
by legendman3
Ok it is still not working. Anyone wanna see why?

Re: How can i divide up a map?

Posted: Thu Feb 09, 2012 12:37 am
by MarekkPie
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.

Re: How can i divide up a map?

Posted: Thu Feb 09, 2012 12:51 am
by legendman3
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.
Thank you! All good here now.

Re: How can i divide up a map?

Posted: Thu Feb 09, 2012 1:42 am
by legendman3
Sorry one more question. (Agh! What is it this time legend?) Well everything is working as it is suppose to but somewhere the goal strayed a little and now it doesn't work as i originally planned.

See it just searches to see if it can find a matching color. It doesn't see if that color is a specific color it just sees if they match. Like it could be green, red, blue, or whatever and all it will print is, "Why hello there." But i was going to have it so that each state was a different color and that each color did something different. but when i tried that all they did was print the same thing.

I need this thing to check and see if it is a specific color matching. So i tried this(because i don't know what im doing exactly):

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
But that doesn't work. (It does give a good idea what i want it to do right?)

Re: How can i divide up a map?

Posted: Thu Feb 09, 2012 2:11 am
by MarekkPie
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

Re: How can i divide up a map?

Posted: Thu Feb 09, 2012 2:22 am
by legendman3
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
Thanks. I only understand when i have to figure it out by myself or it just gets too easy...

Re: How can i divide up a map?

Posted: Thu Feb 09, 2012 2:45 am
by MarekkPie
I should say..."state" works fine, but there could be other things in the extra code that won't wokr.