How can i divide up a map?

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.
User avatar
legendman3
Citizen
Posts: 68
Joined: Sun Jan 22, 2012 8:29 pm

Re: How can i divide up a map?

Post by legendman3 »

Thank you both of you.
User avatar
MarekkPie
Inner party member
Posts: 587
Joined: Wed Dec 28, 2011 4:48 pm
Contact:

Re: How can i divide up a map?

Post 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
User avatar
legendman3
Citizen
Posts: 68
Joined: Sun Jan 22, 2012 8:29 pm

Re: How can i divide up a map?

Post 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.
User avatar
legendman3
Citizen
Posts: 68
Joined: Sun Jan 22, 2012 8:29 pm

Re: How can i divide up a map?

Post by legendman3 »

Ok it is still not working. Anyone wanna see why?
Attachments
map.love
(104.98 KiB) Downloaded 69 times
User avatar
MarekkPie
Inner party member
Posts: 587
Joined: Wed Dec 28, 2011 4:48 pm
Contact:

Re: How can i divide up a map?

Post 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.
Attachments
map_1.love
(105.81 KiB) Downloaded 83 times
User avatar
legendman3
Citizen
Posts: 68
Joined: Sun Jan 22, 2012 8:29 pm

Re: How can i divide up a map?

Post 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.
User avatar
legendman3
Citizen
Posts: 68
Joined: Sun Jan 22, 2012 8:29 pm

Re: How can i divide up a map?

Post 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?)
User avatar
MarekkPie
Inner party member
Posts: 587
Joined: Wed Dec 28, 2011 4:48 pm
Contact:

Re: How can i divide up a map?

Post 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
User avatar
legendman3
Citizen
Posts: 68
Joined: Sun Jan 22, 2012 8:29 pm

Re: How can i divide up a map?

Post 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...
User avatar
MarekkPie
Inner party member
Posts: 587
Joined: Wed Dec 28, 2011 4:48 pm
Contact:

Re: How can i divide up a map?

Post by MarekkPie »

I should say..."state" works fine, but there could be other things in the extra code that won't wokr.
Post Reply

Who is online

Users browsing this forum: No registered users and 51 guests