Code optimization

I need a little help. If I run the following code I can see a little flicker. It actually happens at the time of table.insert I can guess. Is there any way to optimize the code so there will be no flickering.

--Main

supportedOrientations(PORTRAIT_ANY)

function setup()

    displayMode(FULLSCREEN)  
    objectTable = {}
    lastGenerated = ElapsedTime
    interval = 1
    num = 1  
    table.insert(objectTable,Objects(math.random(1,WIDTH-1),math.random(1,4),num))
   
end

function draw()
  
    spriteMode(CENTER)
    sprite("Cargo Bot:Opening Background",WIDTH/2,HEIGHT/2,WIDTH,HEIGHT)
    pushStyle()

    spriteMode(CENTER)
    randomObjectGenerate()   
    popStyle()       
end

function randomObjectGenerate()
    if ElapsedTime - lastGenerated > interval then
        lastGenerated = ElapsedTime
        num = num + 1
        table.insert(objectTable,Objects(math.random(1,WIDTH-1),math.random(1,4),num))   
    end
    for k,v in pairs(objectTable) do 
        v:draw()    
        if v.a.y < 0 or (v.a.x > WIDTH or v.a.x< 0) or v.a.info.id == 0 then
            table.remove(objectTable,k)
            v.a:destroy()
            v.a = nil
            v=nil           
        end             
    end
end


--Objects

Objects = class()

function Objects:init(x,cat,id)
    -- you can accept and set parameters here
    
    self.a = physics.body(CIRCLE,20)
    self.a.x = x
    self.a.y = HEIGHT
    self.a.gravityScale = 1
    self.a.restitution = 0.0
    self.a.interpolate = true
    self.asleepingAllowed = true
    self.flag = true
    self.category = cat
    self.a.info = {}
    self.a.info.id = id
    
end

function Objects:draw()
    pushMatrix()
    translate(self.a.x, self.a.y)
    if self.category == 1 then
        sprite("Cargo Bot:Condition Blue",0,0,50,50)
    elseif self.category == 2 then
        sprite("Cargo Bot:Condition Green",0,0,50,50)
    elseif self.category == 3 then
        sprite("Cargo Bot:Condition Red",0,0,50,50)
    elseif self.category == 4 then
        sprite("Cargo Bot:Condition Yellow",0,0,50,50)         
    end
    popMatrix()
end
    
function Objects:touched(touch)
    -- Codea does not automatically call this method
end


I’ve move all the draw() at the beginning of the function so they are all done without interruption. Then the cleaning or creating is done. Looks much better i think.

function randomObjectGenerate()
    for k,v in pairs(objectTable) do 
        v:draw()    
    end
    if ElapsedTime - lastGenerated > interval then
        lastGenerated = ElapsedTime
        num = num + 1
        table.insert(objectTable,Objects(math.random(1,WIDTH-1),math.random(1,4),num))   
    end
    for k,v in pairs(objectTable) do 
        if v.a.y < 0 or (v.a.x > WIDTH or v.a.x< 0) or v.a.info.id == 0 then
            table.remove(objectTable,k)
            v.a:destroy()
            v.a = nil
            v=nil           
        end             
    end
end

Thanks Jmv. That’s a little better but not as smooth as I was looking for. Specially when I put gravity scale to higher value for example 1 them u can see the difference. But thanks a lot.

Hi @rashedlatif

I changed your code a little. See if this works better for you. I changed the interval from 1 to .2 just to get more objects falling at the same time to check for flicker. Change it back to what you want. Compare this code to what you had to see code that isn’t needed.

EDIT:replaced code with another version


supportedOrientations(PORTRAIT_ANY)
displayMode(FULLSCREEN)
  
function setup()
    objectTable = {}
    lastGenerated = ElapsedTime
    interval = .2
    num = 1
    table.insert(objectTable,Objects(math.random(1,WIDTH-1),math.random(1,4),num))
end

function draw()
    spriteMode(CENTER)
    sprite("Cargo Bot:Opening Background",WIDTH/2,HEIGHT/2,WIDTH,HEIGHT)
    randomObjectGenerate() 
    for k,v in pairs(objectTable) do 
        v:draw() 
    end         
end

function randomObjectGenerate()
    if ElapsedTime - lastGenerated > interval then
        lastGenerated = ElapsedTime
        num = num + 1
        table.insert(objectTable,Objects(math.random(1,WIDTH-1),math.random(1,4),num))   
    end
    for k,v in pairs(objectTable) do 
        if v.a.y < 0 or (v.a.x > WIDTH or v.a.x< 0) or v.a.info.id == 0 then
            table.remove(objectTable,k)
            v.a:destroy()
            v.a = nil
            v=nil           
        end             
    end
end


--Objects

Objects = class()

function Objects:init(x,cat,id)
    -- you can accept and set parameters here
    
    self.a = physics.body(CIRCLE,20)
    self.a.x = x
    self.a.y = HEIGHT+50
    self.a.gravityScale = 1
    self.a.restitution = 0.0
    self.a.interpolate = true
    self.a.sleepingAllowed = true
    self.flag = true
    self.category = cat
    self.a.info = {}
    self.a.info.id = id
    
end

function Objects:draw()
    if self.category == 1 then
        sprite("Cargo Bot:Condition Blue",self.a.x,self.a.y,50,50)
    elseif self.category == 2 then
        sprite("Cargo Bot:Condition Green",self.a.x,self.a.y,50,50)
    elseif self.category == 3 then
        sprite("Cargo Bot:Condition Red",self.a.x,self.a.y,50,50)
    elseif self.category == 4 then
        sprite("Cargo Bot:Condition Yellow",self.a.x,self.a.y,50,50)         
    end
end
    
function Objects:touched(touch)
    -- Codea does not automatically call this method
end