Game is laggy

Hey,

In my app, there are objects that are flying in the air endlessly. After about 5 minutes, the objects begin to lag, everything goes very slow. Is there any way to fix this??

Thanks!
Help is needed.

  • Michael [import]uid: 23689 topic_id: 27081 reply_id: 327081[/import]

I am sure there is a way to fix this, but this greatly depends on your code.
For example, do you create new objects or these are the same objects that you initiated at the beginning of the game?
If you create new objects then you should remove them when you don’t need them any more (like when they are off screen)… Well there are many things that can go wrong when coding, memory leaks (failing to delete data/objects no longer needed) is the first that comes to mind… [import]uid: 80100 topic_id: 27081 reply_id: 109950[/import]

The last time I had this problem was because i ran a couple of eventListeners, and forgot to remove them. So after a couple of minutes, i had hundreds of listeners running and everything became very slow.

It might worth checking out :slight_smile: [import]uid: 50459 topic_id: 27081 reply_id: 110005[/import]

Hey,

Thanks a lot. I believe the problem is because I am not removing the images when they are off the screen. In my app I am propelling images on the screen. After a while it get’s laggy, so how can I remove the image after it goes off the screen?

THANKS!

Here is my code:

[code]
function fly()

flyRan = math.random(1,2)

if flyRan == 1 then

gopz = math.random(-2000, 2000)
local ted = display.newImageRect(“teddy.png”,200,200)
ted.x = -100
ted.y = math.random(150,250)
physics.addBody(ted,“dynamic”, {isSensor = true, density = .5})
ted:applyLinearImpulse(150, -170, ted.x, ted.y )
ted:applyTorque(gopz)
end

if flyRan == 2 then
gop = math.random(-2000, 2000)
local dice = display.newImageRect(“dice.png”,200,200)
dice.x = -100
dice.y = math.random(150,250)
physics.addBody(dice,“dynamic”, {isSensor = true, density = .55})

dice:applyLinearImpulse( 130, -170, dice.x, dice.y )
dice:applyTorque( gop)
end

end
workHUH = math.random(300,500)
row = timer.performWithDelay(workHUH, fly,0)
[/code] [import]uid: 23689 topic_id: 27081 reply_id: 110009[/import]

When the objects are offscreen are they dead (no longer relevant to the game)?

I would put all the objects I’m making in a display group (display.newGroup()) and check if it’s children are offscreen every few seconds. Call obj:removeSelf() on the display object to remove the object and make sure your interating through your group in reverse (otherwise you skip objects when your removing the one before it)

for i=group.numChildren, 1, -1 do  
 local child = group[i]  
 if(child is offscreen) then  
 child:removeSelf()  
 end  
end  

and when your done with all the objects you can just remove the group itself and it will remove the objects, so it makes it simple!

I believe there was a tutorial on this a while ago on how to properly manage objects and remove them

Alternatively and a much better way to do it is you can recycle objects that are offscreen and cause them to re-spawn at the original location, that way you are not creating/deleting objects and you keep a static table of x amount of elements so as not to produce to many [import]uid: 88628 topic_id: 27081 reply_id: 111162[/import]

Ernest said:
Alternatively and a much better way to do it is you can recycle objects that are offscreen and cause them to re-spawn at the original location, that way you are not creating/deleting objects and you keep a static table of x amount of elements so as not to produce to many

Which is exactly how I did it in my app. I had the exact same problem as MichaelAssadi, so I switched to using a table with a set number of objects and as they disappear off one side of the screen they are reset back to the other side with a different set of parameters (my objects are all random). Isped the app up no end and eliminated all signs of lag that I had.

[import]uid: 7841 topic_id: 27081 reply_id: 111345[/import]