How to Spawn multiple objects every 10 seconds

How to make this spawn ‘math.random(1,3)’ smile.png every 10 seconds , and delete the smile.png after the left screen

[code]
local physics = require (“physics”);
physics.start();

local function listener(me)
transition.to (me, {time = math.random(1000,4000), x = math.random(10,310), y = -30, onComplete = function()listener(me)end});
end

–Spawning multiple objects in randoms locations
local function spawnsmile()

local smile = display.newImageRect(“smile.png”, 45, 45);
smile:setReferencePoint(display.CenterReferencePoint);
smile.x = math.random(-10, 400);
smile.y = -40;
transition.to( smile, {time = math.random(2000, 8000), x = math.random(-10, 400) , y = 600,});
physics.addBody(smile, “dynamic”, {density = 0.1, bounce = 0.1, friction = .1, radius = 0});

–Adding touch event
smile:addEventListener(“touch”, smile);
end
tmr = timer.performWithDelay(0, spawnsmile, total_smiles);

[code]
Regards Kevin [import]uid: 225288 topic_id: 35812 reply_id: 335812[/import]

Hi Kevin,
You want to spawn multiple objects every 10 seconds, correct? And then those objects transition-move and then clear when their transition is complete? Try this bit of code:

local physics = require ("physics");  
physics.start();  
  
local numberSmiles = 15 --local variable; amount can be changed  
   
local function clearSmile( thisSmile )  
 display.remove( thisSmile ) ; thisSmile = nil  
end  
  
local function spawnSmiles()  
  
 for i=1,numberSmiles do  
 local smile = display.newImageRect("smile.png", 45, 45);  
 -- smile:setReferencePoint(display.CenterReferencePoint); --not necessary; center is default  
 smile.x = math.random(-10, 400);  
 smile.y = -40;  
 transition.to( smile, { time=math.random(2000,8000), x=math.random(-10,400) , y=600, onComplete=clearSmile } );  
 physics.addBody( smile, "dynamic", { density=0.1, bounce=0.1, friction=0.1, radius=0 } );  
  
 --Adding touch event  
 smile:addEventListener( "touch", touchSmile );  
 end  
  
end  
  
timer.performWithDelay( 10000, spawnSmiles, 0 ) --fire every 10 seconds  

This example uses the transition “onComplete” parameter to call the (smile) clear function. If you don’t want to clear the smiles after the transition, but instead when they leave the screen, you’ll have to implement a sensor setup to detect when they exit the screen, or track them in some other fashion to know when they’re off the screen and then clear them on that condition.

Hope this helps,
Brent [import]uid: 200026 topic_id: 35812 reply_id: 142459[/import]

Thanks a lot Brent :smiley: !! [import]uid: 225288 topic_id: 35812 reply_id: 142477[/import]

Hi Kevin,
You want to spawn multiple objects every 10 seconds, correct? And then those objects transition-move and then clear when their transition is complete? Try this bit of code:

local physics = require ("physics");  
physics.start();  
  
local numberSmiles = 15 --local variable; amount can be changed  
   
local function clearSmile( thisSmile )  
 display.remove( thisSmile ) ; thisSmile = nil  
end  
  
local function spawnSmiles()  
  
 for i=1,numberSmiles do  
 local smile = display.newImageRect("smile.png", 45, 45);  
 -- smile:setReferencePoint(display.CenterReferencePoint); --not necessary; center is default  
 smile.x = math.random(-10, 400);  
 smile.y = -40;  
 transition.to( smile, { time=math.random(2000,8000), x=math.random(-10,400) , y=600, onComplete=clearSmile } );  
 physics.addBody( smile, "dynamic", { density=0.1, bounce=0.1, friction=0.1, radius=0 } );  
  
 --Adding touch event  
 smile:addEventListener( "touch", touchSmile );  
 end  
  
end  
  
timer.performWithDelay( 10000, spawnSmiles, 0 ) --fire every 10 seconds  

This example uses the transition “onComplete” parameter to call the (smile) clear function. If you don’t want to clear the smiles after the transition, but instead when they leave the screen, you’ll have to implement a sensor setup to detect when they exit the screen, or track them in some other fashion to know when they’re off the screen and then clear them on that condition.

Hope this helps,
Brent [import]uid: 200026 topic_id: 35812 reply_id: 142459[/import]

Thanks a lot Brent :smiley: !! [import]uid: 225288 topic_id: 35812 reply_id: 142477[/import]

 Ineed help too I want random images to spawn every 480 pixels and remove themselves

 Ineed help too I want random images to spawn every 480 pixels and remove themselves