0
votes

I have asked a question similar to this, but i was asking it for Processing.JS. Now i am making something out of LOVE2D Lua, what I'm trying to do is that i have a button where when clicked it adds a circle on-screen. I already have code that when i click and hold on the circle, i can move it around. But when i add a second circle, they both are using he same variables. I want it so i write once instance of the code to move and add the circle, but i can call it multiple times and each one be unique without writing code to anticipate an infinite amount of circles. Here is my code:

obn = 0
ellipsex = 50
ellipsey = 50
ellipsew = 50
ellipseh = 50
ellipser = 255
ellipseg = 0
ellipseb = 0
function love.draw()
mousex, mousey = love.mouse.getPosition()
for i=0,obn,1 do
ellipse()
end
end


function love.mousereleased(x, y, button)
   if button == 2 then
      obn = obn + 1
   end

end




function love.update(dt)
    if love.mouse.isDown(1) then
           if mousex > ellipsex and mousex < ellipsex + ellipsew and mousey > ellipsey and mousey < ellipsey + ellipseh then
ellipsex = mousex
ellipsey = mousey
end
    end
end





function ellipse()






love.graphics.setColor(ellipser, ellipseg, ellipseb)
love.graphics.ellipse("fill", ellipsex, ellipsey, ellipsew, ellipseh)

end

But when i right-click to add a circle(increment how many times the for-loop runs) it doesnt add a second circle for me to move independent from the first one. Help?

1

1 Answers

0
votes

The way I would go about handling multiple instances of ellipses is creating a table for every ellipse that would hold it's properties and a draw function.

local ellipses = {} -- store all ellipses into a table

function create_ellipse(x,y,w,h,r,g,b)
    local ellipse = {
        x = x,
        y = y,

        w = w,
        h = h,

        r = r,
        g = g,
        b = b
    }

    function ellipse.draw()
        love.graphics.setColor(ellipse.r,ellipse.g,ellipse.b)
        love.graphics.ellipse("fill",ellipse.x,ellipse.y,ellipse.w,ellipse.h)
    end

    ellipses[#ellipses+1] = ellipse -- insert new ellipse into ellipses table

    return ellipse
end

function love.draw()
    for i = 1,#ellipses do
        ellipses[i].draw(); -- call each ellipse's separate draw function
    end
end

function love.mousereleased(x,y,button)
    if button == 2 then
        create_ellipse(x,y,50,50,255,0,0) -- bonus: every ellipse is created where the user clicked
    end
end

function love.update(dt)
    if love.mouse.isDown(1) then
        local mousex,mousey = love.mouse.getPosition() -- there is no need to request the mouse position every frame, but only when a user clicks anywhere on the screen

        for i = 1,#ellipses do
            local current_ellipse = ellipses[i]

            if mousex >= current_ellipse.x and mousex <= current_ellipse.x+current_ellipse.w and mousey >= current_ellipse.y and mousey <= current_ellipse.y+current_ellipse.h then
                current_ellipse.x = mousex
                current_ellipse.y = mousey
            end
        end
    end
end

You could even make your own Ellipse class if you're into OOP.