function spawnTree(nb)
for i = 0, nb-1 do
Tree = {}
Tree.x = math.random(0, love.graphics.getWidth())
Tree.y = math.random(0, love.graphics.getHeight())
table.insert(Trees, Tree)
love.graphics.draw(Sprites.Tree1, Tree.x, Tree.y)
end
end
There are a few things... First, you are calling your create function inside love.draw, which means it will be called every single frame. Also, you're calling the draw function inside the constructor function, that's why it flashes... It only shows for one frame. Here's a small example:
local trees = {}
function createTree(x, y)
-- make this local just because... it avoids global conflicts later
local tree = {
x = x or math.random(0, love.graphics.getWidth()),
y = y or math.random(0, love.graphics.getHeight()),
}
table.insert(trees, tree)
end
function love.draw()
for i, tree in ipairs(trees) do
-- just using a rectangle for now bc I don't have your sprites
love.graphics.setColor(20,200,20)
love.graphics.rectangle("fill", tree.x, tree.y, 50, 100)
end
end
function love.keypressed(k)
if k == "return" then
createTree()
end
end