How to display moving particles in Background with no Hanging of game

Hey guys, 

I have applied the following code for moving the particles in background. But this codes make the game slow in few minutes. 

local function onTimer() if pauseGame~=true then local particleSystem = physics.newParticleSystem{ filename = "images/particle.png", radius = math.random( 2, 5 ), imageRadius = math.random( 2, 5 ) } particleSystem.myName = "myParticleSystem" local particleParams = { flags = { "water" }, velocityX = 40, velocityY = 0, color = { 1, 1, 1, 0.4 }, x = math.random( 10, 750 ), y = math.random( 10, 1000 ) } particleSystem:createParticle( particleParams ) end end particletimer=timer.performWithDelay( 1000, onTimer, -1)

Well, I suspect there are to many particles (nad/or particle systems) on screen, so the game slows down.

You also create a new particle system every second, instead of a new particle in an already existing one.

Maybe try something like this?

local particleSystem = physics.newParticleSystem{ filename = "images/particle.png", radius = math.random( 2, 5 ), imageRadius = math.random( 2, 5 ) } particleSystem.myName = "myParticleSystem" local particleParams = { flags = { "water" }, velocityX = 40, velocityY = 0, color = { 1, 1, 1, 0.4 }, x = math.random( 10, 750 ), y = math.random( 10, 1000 ) } local function onTimer() if pauseGame~=true then particleSystem:createParticle( particleParams ) end end particletimer = timer.performWithDelay( 1000, onTimer, -1)

One issue with that is that all particles will now be the same size, whereas in msahil’s code each particle would have its own random radius.

Do you ever delete particles when they go off screen? If not, then that would play a big part in the slowdown as you would have more and more every second.

Well, I suspect there are to many particles (nad/or particle systems) on screen, so the game slows down.

You also create a new particle system every second, instead of a new particle in an already existing one.

Maybe try something like this?

local particleSystem = physics.newParticleSystem{ filename = "images/particle.png", radius = math.random( 2, 5 ), imageRadius = math.random( 2, 5 ) } particleSystem.myName = "myParticleSystem" local particleParams = { flags = { "water" }, velocityX = 40, velocityY = 0, color = { 1, 1, 1, 0.4 }, x = math.random( 10, 750 ), y = math.random( 10, 1000 ) } local function onTimer() if pauseGame~=true then particleSystem:createParticle( particleParams ) end end particletimer = timer.performWithDelay( 1000, onTimer, -1)

One issue with that is that all particles will now be the same size, whereas in msahil’s code each particle would have its own random radius.

Do you ever delete particles when they go off screen? If not, then that would play a big part in the slowdown as you would have more and more every second.