Randomly generated chests

I made randomly generated chest and stored them in a table, but for some reason after they generate they only draw for a single frame here is my chest code (There’s a way to make the code easier to read but idk how to do it)

Chest = class()

function Chest:init()
self.x = x
self.y = y
self.chestTime = chestTime
chests={}
table.insert(chests,vec2(x, y))

end

chestTime = ElapsedTime+1

function Chest:draw()

         if ElapsedTime>chestTime then

for a,b in pairs(chests) do
sprite(“Planet Cute:Chest Closed”, math.random(0, WIDTH), math.random(0, HEIGHT))

chestTime = ElapsedTime+1
end
end
end

Shouldn’t it be

if ElapsedTime>self.chestTime then

Pretty much as soon as ElapsedTime>chestTime it draws, but then you set chestTime to ElapsedTime+#chests so it doesn’t draw them the next frame. Btw put 3 of ‘~’ before and after your code.

@jlpt159 Here’s an example of drawing 50 chests at random screen positions.

displayMode(FULLSCREEN)

function setup()
    c=Chest(50)
end

function draw()
    background(0)
    c:draw()
end

Chest = class()

function Chest:init(nbr)
    self.chests={}
    for z=1,nbr do
        table.insert(self.chests,vec2(math.random(WIDTH),math.random(HEIGHT)))
    end
end

function Chest:draw()
    for a,b in pairs(self.chests) do
        sprite("Planet Cute:Chest Closed",b.x,b.y,30)
    end
end

Thanks guys for helping me :smiley: