3D Particle Effect – Flash CS4 Tutorial

December 21st, 2008 | 44 Comments

In today's video tutorial, I'll show you how to create an ActionScript 3D particle effect that causes multiple stars to animate out in all directions in 3D space. Here is the ActionScript for this tutorial:

var container:MovieClip = new MovieClip();
addChild(container);

root.transform.perspectiveProjection.fieldOfView = 120;

addEventListener(Event.ENTER_FRAME, addStar);

function addStar(e:Event):void
{
	var mc:star = new star();
	mc.x = stage.stageWidth / 2;
	mc.y = stage.stageHeight / 2;
	mc.z = 0;
	mc.xVel = Math.random() * 4 - 2;
	mc.yVel = Math.random() * 4 - 2;
	mc.zVel = Math.random() * 4 - 2;
	mc.count = 0;
	container.addChild(mc);
	mc.addEventListener(Event.ENTER_FRAME, animate);
}

function animate(e:Event):void
{
	e.currentTarget.x += e.currentTarget.xVel;
	e.currentTarget.y += e.currentTarget.yVel;
	e.currentTarget.z += e.currentTarget.zVel;
	e.currentTarget.count++;
	if (e.currentTarget.count >= 100)
	{
		e.currentTarget.removeEventListener(Event.ENTER_FRAME, animate);
		container.removeChild(MovieClip(e.currentTarget));
	}
}
Tags: , , , ,

44 Responses

  1. Mearvin says:

    Awesome tutorial…..It has really helped a lot…thanks a lot…..keep going!!!

  2. Jon Mulligan says:

    It would be great if the stars would topple end over end randomly, that would be awesome!

    Great tutorial.

  3. Zenseeker says:

    Thanks so much for your great tutorials. Just wanted to let you know someone really appreciates them.

  4. Rapi says:

    Wow. Thanks for that great tutorial. Yeah, what zenseeker said. This is too cool. :) Can’t wait to play around with it.

  5. Sluff says:

    Hey, this is a great tutorial man! Thanks a lot :D

  6. cretzuuu says:

    i have a bit of a problem … i get this error back saying this:
    1119: Access of possibly undefined property perspectiveProjection through a reference with static type flash.geom:Transform. ->
    root.transform.perspectiveProjection.fieldOfView = 120;

  7. cretzuuu says:

    sorry for double posting… but i’m in a very big hurry…and i don’t know how to come to a solution… help please

  8. Sagir says:

    Hi Craig,

    Great Job!

    once you maximize the window the center does not propogate to center,

    the instance of star movieclip “mc” changes you might have noticed.

    thank you,

    Sagir

  9. J Z says:

    Great tutorial. You are an awesome teacher.

  10. martin says:

    how can I add text without it being behind the effect.

  11. steve says:

    hello, great tutorial

    however i was inputting the codes and i thought i had it all set right,and i tested it and i only had the single star the in middle of the screen????

  12. Andrew says:

    Thanks for the tutorial!

    However I was wondering how I could incorporate the effect into a tween. I would like to have the effect motion tween over a background layer. Is this possible?

    Thanks,
    -A

  13. Joe says:

    Great tutorial.

    How can I have this play behind other graphics on the stage? I have a logo centered on the stage and what similiar stars behind the logo.

  14. Michelle says:

    awesome job and excellent video explanation

  15. Manndy says:

    Man you just rock. I am really happy to visit your website.
    I know how to do lots of interesting effects in after effects but when it comes down to flash then my knowledge is not so good. But know i think that i can do it in flash also!!! Please post some more tutorials. The best part of this tutorial is that everything is done using as3.

  16. Alex says:

    Excellent tutorial! I have a question about how to unload this when a user hits a replay button. The 3D particle effect stays on screen. What code should I add and where?

  17. Karthi says:

    i’m new to as3 thanks for yur tutorial
    very few lines yet so fabulous understanding thanks a lot

  18. lektira says:

    thx i will use it on my future project

  19. corinne says:

    Thank you very much !

  20. Kanishka says:

    Nice tutorial

    It does not appear in the centre of the window when it’s maximized
    Thanks

  21. chico crevin says:

    Scuse-me I don’t write enghish very well.
    How to put this program without the loop and place not on stage but in a window of 200px by 500px

  22. Eddy says:

    Hi,
    great tutorial for a newbie like me :)
    is it also possible to have this on a timer even instead of the enter frame event ? i added a button to start the animation but i can’t figure out how to access the variables outside the function. I would like the animation to start with my button and run for a certain amount of seconds.
    Any push in the right direction would be great :)
    Eddy.

  23. Turkis says:

    Thenks Man

  24. Greg says:

    Excellent tutorial!
    Can’t wait to try it.

  25. Luz Elena says:

    Saludos desde Barcelona, ¡¡¡ Exelentes tutoriales !!!!

    Gracias :)

  26. JJ says:

    Should be like this (for centering)
    mc.y = stage.height / 2;
    mc.y = stage.height / 2;

  27. John says:

    @ manndy…obviously you didnt read the top of the page…this is done in cs4 and not cs3 or as3 as you out it lol

    Great tutorial mate thanks

  28. John says:

    btw once you export the movie and then go fullscreen it stays in the middle

  29. i have a bit of a problem … i get this error back saying this:
    1119: Access of possibly undefined property perspectiveProjection through a reference with static type flash.geom:Transform. ->
    root.transform.perspectiveProjection.fieldOfView = 120;

  30. nailer says:

    Great job Craig. I’m just starting out and have learned much from watching you. The effect applied to your image in the chair is very intersting. Is there a tutorial for that?

  31. elevated says:

    As always you are helpful as ever with a great tutorial. Can you or will you pass on the “how” of the transition effect you used for the image at the clip start.

  32. Tracey says:

    Thanks so much! I really enjoyed this. I don’t even have cs4 yet, but i can’t wait (now that cs5 is out, oh well). I am just now learning actionscript so it was great to be able to understand what your directives were.

  33. EssellGop says:

    be happy and love. kiss

  34. DancingKim says:

    Howdy, i’m a professional dancer. i’d like to make a showreel to get my promotions. I also prefer to use some animation. Can someone suggest me a good animation studio, but possibly not very expensive? I’m here for 3 months for a tour.
    Love
    Kim.

  35. great tutorial on cool effect. thanks and keep it comming!

  36. profeivan says:

    Great tutorial man. Well explained. Thanks for taking your time to share this with the Flash community. Best regards from Colombia

  37. Anonymous says:

    Another great tutorial! Though, I wonder if it’s possible to fade out the particles before actually removing them.. I’ve experimented different methods, but I haven’t succeeded yet. The reason being my being quite new to actionscript 3.0..

  38. Elza Pereira says:

    Your tutorial is excellent. I am developing my portfolio and I believe in using particles.
    I am Brazilian, I am learning English because I love Flash/AS3.
    Thank you. Big hug.

  39. Chris says:

    Thank you so much good sir. It’s nice to find an advanced tutorial like this that actually works. Good AC3 is hard to find. The explanation that flows through the tutorial is also very intuitive and descriptive.

  40. Julie says:

    I just wanted to thank you for this tutorial. !t was so helpful! I look forward to watching others.

  41. ceriss says:

    can you please post a tutorial regarding the effects you create in making your picture in the start of the video..thanks(^^,)

Leave a Reply