Flash CS3 Particle Effect Tutorial

3. Animate the particle in ActionScript. To animate our particle, we’re going to create a function that changes our X and Y coordinates ever-so-slightly. Then we’re going to create an ActionScript Timer that causes this moveParticle function to run over and over again at a specified rate. Go back to your Actions Panel, and enter the following code:

Lines 6 - 10 contain a function that simply increases the X and Y coordinates of the movie clip by 3 pixels each. And when we use the Timer to run this function over and over again, we’ll achieve the effect of constant movement down (increasing Y value) and to the right (increasing X).

To create the Timer (line 12), we create a new instance of the Timer class and store it in a variable, which I’ve called “myTimer”. The “50″ inside the parentheses of the Timer() represents how often we want the Timer to trigger an event. This number is in milliseconds.

Once the timer is created, we need to add an event listener to the timer (line 13). This “listener” listens for every single time the Timer is triggered (every 50 milliseconds in this case) and then causes the “moveParticle” function to run every time it’s triggered.

But the Timer won’t actually start working until we tell it to, and that’s what we’re doing in line 14.

Note: The speed of movement is determined by two things: (1) the distance moved every time the moveParticle function is called, and (2) the frequency with which the function is called. You can alter the speed of the animation by changing any combination of these two numbers.

4. Random Movement. One of the features of an effective particle effect is randomization of a number of different factors, including movement, size, opacity, etc. Let’s play with our code a little in order to randomize the movement.

Right now, every time the moveParticle function is called, the particle is moving to the right three pixels (dot.x+=3) and down three pixels (dot.y+=3). Well, we may not always want the particle to move at that exact speed or in that direction. So let’s change this so that it randomly picks a number between -4 and +4. (A negative “x” value will move the particle to the left and a negative “y” value will move it up.)

In the example code below, I’ve updated it to reflect these random numbers:

On lines 6 and 7, I’ve created two random numbers to represent the rate of x and y movement. The reason we don’t want to calculate this number inside the moveParticle function is because we want our particle to move in a constant direction. If we calculated the random number inside the function, then every time the function was called by the Timer, it would calculate a different random number, and the particle would constantly be changing directions.

So, what about those random numbers? How does that work?

Well, the Math.random() function returns a random number between 0 and 1. So, to get a number between 0 and 8, you would multiple the result by 8. When you subtract 4 from the final result, you get a random number between -4 and +4.

At this point, every time you test your movie, your particle will be moving in a random direction and at a random speed. To increase the highest possible speed, simply alter the random range. For example, if you wanted a random number between -10 and +10, you would use “Math.random() * 20 - 10″.

On the next page, we’ll take a look at how to add multiple particles to the stage.

Tags: ,

Pages: 1 2 3 4


You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

AddThis Social Bookmark Button

17 Responses to “Flash CS3 Particle Effect Tutorial”

  1. Igor Borodin Says:

    Thank you. Very nice tutoring style - clean, coherent, well worded.
    IB

  2. […] Flash CS3 Particle Effect Tutorial […]

  3. good day!

    first of all, thnx for this wonderful tutorial…

    i wonder how to make this particle effect as mask of my pictures…coz i wanted this effect as the transitions of my photo gallery…could someone help me with this?

  4. I’ve been working on an open source particle engine aimed at designers. It lets you create effects like this (and even more complex behaviors) through an interactive particle explorer without writing any Actionscript.

    http://www.rogue-development.com/pulseParticles.html

  5. G’Day Craig,

    Mate, I’ve been playing with Flash for about 9 months, and only got enough courage to start playing with actionscript about 6 months ago. I struggled for about 3 months and then went off it.

    Then about 1 month ago I stumbled over your video tutorials and they just make it all so clear and simple. I have only two days ago upgrade from V8 to CS3 (i.e. actionscript 2 to actionscript 3). I have only had a small taste of whats to come, have been watching your tutorials on AS3 now. Loving every minute of it.

    Anyways, just thought you deserve a rap for your efforts.

    Thanks.

  6. This a great effect that has tons of potential.

  7. Let me just say that this is the most thorough Flash tutorial that I’ve ever attempted. Thanks a bunch for the post…this site is going into favorites list immeditealy!

  8. This was great. Please make more tutorials as this was the only one I could follow on the web!!

  9. Cynthia Schmutz Says:

    Hi Craig,
    I finished your tutorial (great btw) but I was wondering if you could tell me how to control the positioning with your cursor? Thanks!

  10. Great tutorial :)

    and if I can help a bit to answer Cynthia Schmutz question.
    To control it with mouse you just need to modify 2 lines in the addParticle function:

    dot.x = mouseX;
    dot.y = mouseY;

    Im No an expert since I only have 5 days of experience but it works.

  11. Its a very good tutorial But I would like to know, if it is possible to stream .asx radiostations in flash cs3.

    Thanks a lot

  12. Does anyone know how this could be altered to allow 4 different colours?

    Cheers.

  13. I used your particle effect tutorial its great thanks. I tweaked mine and added this:

    dot.x = mouseX;
    dot.y = mouseY;

    How can I make this so that when you mouse over a link the particles don’t interfere with clicking the link?

  14. Can this thing be done in Action Script 2.0? Me and many of my friends want to know this. While dooing it in Action Script 3.0 many of my friends getting error like “playerglobal.swc is missing”. How can this be solved?

  15. Very clean and simple effect, I like it

  16. I got the following error when i run it in my application:
    TypeError: Error #1009: Der Zugriff auf eine Eigenschaft oder eine Methode eines null-Objektverweises ist nicht möglich.
    at bg_partikel_fla::disco_4/addParticle()
    at flash.utils::Timer/_timerDispatch()
    at flash.utils::Timer/tick()

    disco is the movieclip in which i placed the as.

    Thanks for help

  17. […] Read more […]

Leave a Reply