Aye. Here’s an improved version of @SonicX278’s code, without the unnecessary globals, with snowflakes that don’t bump into objects in the scene, and with a method that removes snowflakes as they go outside the screen. Also, I’ve removed the unneeded table in favor of local snowflakes and a group, which is cleaner and won’t fail when the number of snowflakes goes over 15 digits (admittedly rarely, but still… :D).
The loop through each snowflake is required because @SonicX278 used physics. Without physics, we could use a transition and remove the snowflake when it’s done, but with it, we need to know when a snowflake goes outside the screen.
local centerX = display.contentCenterX local centerY = display.contentCenterY local actualW = display.actualContentWidth local actualH = display.actualContentHeight local physics = require( "physics" ) physics.start() local sTimer local BG = display.newRect( centerX, centerY, actualW, actualH ) BG:setFillColor( 0.5, 0.6, 1 ) local snow = display.newGroup() local function checkFlakes() for i = snow.numChildren, 1, -1 do if snow[i].y - snow[i].contentHeight \> actualH then display.remove(snow[i]) end end end local function createSnowflake() local randomSize = math.random( 1, 3 ) local randomX = math.random( centerX - actualH \* 0.5, centerX + actualH \* 0.5 ) local randomLeftRight = math.random( -50, 50 ) local randomDown = math.random( 101, 150 ) local flake = display.newCircle( randomX, centerY - actualH \* 0.5 - 20, randomSize ) physics.addBody(flake, "kinematic") flake.isSensor = true -- So it doesn't collide with objects in the scene flake:setLinearVelocity( randomLeftRight, randomDown ) flake.alpha = 0.8 snow:insert(flake) end sTimer = timer.performWithDelay( 150, createSnowflake, -1 ) Runtime:addEventListener("enterFrame", checkFlakes)
[EDIT]: Sorry, @Rob2, I just realized @SonicX278 and I hijacked the thread to talk about non-CBE particles… To answer the original question, the one-liner should do the trick.