Game Development Reference
In-Depth Information
Let's try animating some circles:
local circles = {}
function love.mousepressed(x, y, button)
if button == "l" then
newCircle = {
size = 0,
n = 20,
x = x,
y= y
}
table.insert(circles, newCircle)
end
end
function love.update(dt)
for _, c1 in pairs(circles) do
c1.size = c1.size + c1.n * dt
end
end
function love.keypressed(key)
if key == " " then
circles = {}
end
end
function love.draw()
for _, c in pairs(circles) do
love.graphics.circle( "line", c.x, c.y, c.size, 100)
end
end
This code makes the circles grow indefinitely. However, we can set a limit for the maximum and
minimum size of the circles.
Let's choose a maximum of 20 and a minimum of 0. To do this, we just need to change the update
function, as follows:
function love.update(dt)
for _, c1 in pairs(circles) do
c1.size = c1.size + c1.n * dt
if c1.size < 0 then
c1.size = 0
c1.n = c1.n * -1
elseif c1.size > 20 then
c1.size = 20
c1.n = c1.n * -1
end
end
end
Search WWH ::




Custom Search