How to make simple particles?

Hi, I’m trying this for a long time without succes :frowning:

I need something like this:
If two objects collide - Object 1 remove self and spawn 5 stars(particles) which move randomly for a while and then disappear. (Exactly the same effect what is in “Samurai fruit” demo, but with my own graphic…)

Can someone help me with this? Thanks.

[import]uid: 59968 topic_id: 13399 reply_id: 313399[/import]

[lua]local particleNum = 100
local function createParticles(e)
for i=1,particleNum do
local circle = display.newCircle(0,0,1)
circle.x = e.x
circle.y = e.y
local x1 = e.x + math.cos(math.random(360)) * math.random(20,70)
local y1 = e.y + math.cos(math.random(360)) * math.random(20,70)
transition.to(circle,{x = x1,y = y1,alpha = 0,time = 500})
end

end
Runtime:addEventListener(“tap”,createParticles)[/lua]
more particleNum more slower perfomance also this code is not removing any object after use
its for simple idea
:slight_smile: [import]uid: 12482 topic_id: 13399 reply_id: 49203[/import]

one more with little modification still i am not removing anything

[lua]local particleNum = 150
local function createParticles(e)
local myRandom = math.random
for i=1,particleNum do
local circle = display.newCircle(0,0,1)
circle:setFillColor(myRandom(255),myRandom(255),myRandom(255))
circle.x = e.x
circle.y = e.y
local x1 = e.x + math.cos(myRandom(360)) * myRandom(20,70)
local y1 = e.y + math.cos(myRandom(360)) * myRandom(20,70)
transition.to(circle,{x = x1,y = y1,alpha = 0,time = 800})
end

end
Runtime:addEventListener(“tap”,createParticles)[/lua] [import]uid: 12482 topic_id: 13399 reply_id: 49206[/import]

Yeah, that is really great, but how to set x and y to object who collide ? [import]uid: 59968 topic_id: 13399 reply_id: 49210[/import]

take a look at my simple particle demo
http://developer.anscamobile.com/code/simple-particle-explosion

it doesn’t use transitions but an enterFrame method for animation.
and you can send x/y coordinates to the explosion-function.

you’ll get the idea…

[import]uid: 70635 topic_id: 13399 reply_id: 49225[/import]

FYI, to remove an object directly after a transition, do this:

transition.to(myObj, {time = 200; alpha = 0; transition=easing.outQuad, onComplete = function () myObj:removeSelf()end ;}) [import]uid: 70635 topic_id: 13399 reply_id: 49226[/import]

Thanks, I made what I need.
If I have object which moving, how can I make particles follow him? (Something like fire from rocket engine…) [import]uid: 59968 topic_id: 13399 reply_id: 49285[/import]

see this link
http://developer.anscamobile.com/forum/2011/08/08/how-make-particles-follow-object [import]uid: 12482 topic_id: 13399 reply_id: 49755[/import]

Explosion that disturbs the surrounding physical objects.

Combining both hgvyas123’s and Canupa’s codes, I needed particles to affect other physical objects, and I got the code below. Thanks everyone. This is indeed one of the easiest explosion/particles/fireworks code around.

[lua]local particleNum = 10
function createParticles(e)
local myRandom = math.random
for i=1,particleNum do
local circle = display.newCircle( 0, 0, 5 )
circle:setFillColor(myRandom(255),myRandom(255),myRandom(255))
circle.x = e.x
circle.y = e.y
local x1 = e.x + math.cos(myRandom(360)) * myRandom(20,70)
local y1 = e.y + math.cos(myRandom(360)) * myRandom(20,70)
physics.addBody( circle, { radius=circle.width*0.5, bounce=1.0 } )
transition.to(circle, {x = x1,y = y1, time = 1000; alpha = 0;
transition = easing.outQuad,
onComplete = function ()
physics.removeBody(circle)
circle:removeSelf()
end ;})
end
end

Runtime:addEventListener(“tap”,createParticles)

----Uncomment this to remove the listener if unused, later on.
–Runtime:removeEventListener(“tap”,createParticles)[/lua] [import]uid: 58387 topic_id: 13399 reply_id: 120253[/import]

Explosion that disturbs the surrounding physical objects.

Combining both hgvyas123’s and Canupa’s codes, I needed particles to affect other physical objects, and I got the code below. Thanks everyone. This is indeed one of the easiest explosion/particles/fireworks code around.

[lua]local particleNum = 10
function createParticles(e)
local myRandom = math.random
for i=1,particleNum do
local circle = display.newCircle( 0, 0, 5 )
circle:setFillColor(myRandom(255),myRandom(255),myRandom(255))
circle.x = e.x
circle.y = e.y
local x1 = e.x + math.cos(myRandom(360)) * myRandom(20,70)
local y1 = e.y + math.cos(myRandom(360)) * myRandom(20,70)
physics.addBody( circle, { radius=circle.width*0.5, bounce=1.0 } )
transition.to(circle, {x = x1,y = y1, time = 1000; alpha = 0;
transition = easing.outQuad,
onComplete = function ()
physics.removeBody(circle)
circle:removeSelf()
end ;})
end
end

Runtime:addEventListener(“tap”,createParticles)

----Uncomment this to remove the listener if unused, later on.
–Runtime:removeEventListener(“tap”,createParticles)[/lua] [import]uid: 58387 topic_id: 13399 reply_id: 120253[/import]