Circles Drawing on top of each other

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.
Post Reply
aswinmohanme
Prole
Posts: 7
Joined: Thu Jul 07, 2016 7:34 am

Circles Drawing on top of each other

Post by aswinmohanme »

Hey there , I am trying to make a simple app in love that spawns circles at the point where the mouse is clicked.

The problem here is that there is only one circle whereas what I want is a number of circles emnating from the point of the click. What seems to be the Issue ?

Thanks in Advance

Here is the Code

Code: Select all

--[[
    - Author :: Aswin Mohan
    - Dated :: 23.July.2016

    - @Description :
            Draw Circles on the screen from last click Point
]]

-- Circle Description
local circles = {}
local circle = {}

-- Initialise Love
function love.load(arg)
    -- Set up the Mouse
    circle.posX = love.graphics.getWidth() / 2
    circle.posY = love.graphics.getHeight() / 2
    circle.radius = math.random(10,50)
    circle.shouldDraw = false
    circle.time = 0

    -- Set the BackGround Color
    love.graphics.setBackgroundColor(52 , 73 ,94 , 255)
end

-- Update before Each Frame
function love.update(dt)
    circle.time = circle.time + dt

    circle.shouldDraw = false
    if circle.time > 1 then
        -- Change Radius
        circle.radius = math.random(10 , 100)
        circle.shouldDraw = true
        circle.speedX = 150 * math.random(-1 , 1)
        circle.speedY = 150 * math.random(-1 , 1)

        -- Create an Array of Circles
        table.insert(circles,circle)

        -- Update the Postion of the Circles Randomly
        for i , v in pairs(circles) do
            v.posX = v.posX + v.speedX * dt
            v.posY = v.posY + v.speedY * dt
        end

        circle.time = 0
    end
end

-- Draw each Frame
function love.draw()
    for i , v in pairs(circles) do
        love.graphics.circle("fill", v.posX, v.posY, v.radius, 20)
    end
end

-- Handle Mouse Clicks
function love.mousepressed(x, y, button, isTouch)
    if button == 1 or button == 2 then
        circle.posX = x
        circle.posY = y
    end
end

-- Clean things up
function love.quit()
    -- body...
end

User avatar
airstruck
Party member
Posts: 650
Joined: Thu Jun 04, 2015 7:11 pm
Location: Not being time thief.

Re: Circles Drawing on top of each other

Post by airstruck »

You've created one "circle" table and one "circles" table. You modify the fields of the "circle" table every frame and append it to the "circles" table. In other words, your "circles" table contains many references to the same "circle" table. This is because Lua always uses pass-by-reference for tables (it only uses pass-by-value for primitives).

You probably meant for the "circles" table to contain many unique tables rather than the same table over and over. In that case, you should create a new "circle" table each frame and append it to the "circles" table rather than reusing the same one.

If that didn't make sense, you might want to read about pass-by-reference vs. pass-by-value.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 6 guests