ActionScript 3.0: Building Particle Systems

ActionScript 3.0: Building Particle Systems

with Todd Perkins

 


From understanding the basics to creating advanced effects like explosions and smoke, ActionScript 3.0: Building Particle Systems covers each step involved in creating particles and building particle systems. Instructor Todd Perkins explains how to make a versatile particle class that controls particle movement, gravity, friction, spin, rotation, fading, scaling, and other movement effects. He also demonstrates how each unique particle system can be added to a real world application. Exercise files accompany the course.

show more

author
Todd Perkins
subject
Developer, Web, Web Development
software
ActionScript 3.0
level
Advanced
duration
1h 22m
released
Apr 11, 2008

Share this course

Ready to join? subscribe


Keep up with news, tips, and latest courses.

submit Course details submit clicked more info

Please wait...

Search the closed captioning text for this course by entering the keyword you’d like to search, or browse the closed captioning text by selecting the chapter name below and choosing the video title you’d like to review.



Introduction
Welcome
00:00Hi, I'm Todd Perkins and aside from being an overall Flash enthusiast,
00:04I've written several books on Flash and I'm an Adobe certified Flash constructor.
00:09In this title, I'm going to walk you through creating particles and particle systems.
00:14Much of my inspiration for this title has come from other ActionScript gurus like Seb Lee-Delisle.
00:20Seb Lee-Delisle has also recorded a title on particles that can be found in the Online Training Library.
00:26Let's take a look at some of the applications we'll be creating in this title.
00:29One of the applications displays the weather and is powered by Yahoo! weather.
00:34If it's raining outside, we have a particle system that displays rain particles.
00:38We'll talk about how to use particles in creative ways. For example the wake from the boat is a particle system,
00:44the splash that the fish makes is a particle system,
00:47and the fish itself here is even a particle.
00:50We'll talk about how to make interactive particles like this particle system that explodes when you click on it.
00:56We'll talk about to create particle effects like smoke and we'll even add particles to video.
01:01So I hope you're excited to learn about how to create particle systems.
Collapse this transcript
Prerequisites
00:00In this title, we're going to be working very fast and I'm going to assume that you know a lot of ActionScript and are very
00:05familiar with the basics of Flash.
00:07So in this movie we'll start with some of the prerequisites for this title.
00:11First, I expect to have a basic knowledge of Flash as taught in Flash CS3 Fundamentals.
00:16You should know how to manipulate objects on the Stage,
00:19had to create movie clips and how to modify their registration points
00:23and a little about working with linkage properties. You should be familiar with the concepts taught in ActionScript 3.0
00:28in Flash CS3 Essential Training and ActionScript 3.0 and Flash CS3 Beyond on the Basics.
00:34We're going to quickly write a lot of code here.
00:36For a lot of the code, I'm actually going be just explaining it, assuming that it's very easy for you to read code and catch
00:42on to how it's working and how you can do the same things.
00:45Another title that's not a prerequisite,
00:47but helpful because we'll be talking about XML a little bit, is ActionScript 3.0: Working with XML.
00:53And so know you're familiar with what I expect you to know, let's get started.
Collapse this transcript
Using the exercise files
00:00If you're premium subscribers to lynda.com or have this title on a disc, then you have access to the exercise files.
00:07The exercise files in this title are organized by chapters.
00:11Each chapter is organized by movies.
00:13Throughout the title, I'll be saying we are working in, for example, the Chapter 4 folder for that chapter. Then I'll say we're
00:19working in the movie one folder or the movie two folder, etc.
00:23Most of the folders have start and final folders.
00:26The start folders contain the initial states of the files when we start working on that exercise.
00:30The final folders contain the final state of the files after the exercise is complete.
00:34If you have access to the exercise files, don't worry, you can still follow along. I'll explain as we go along how to setup
00:41your files similarly to the files we're working on and I'll walk you through all the code
00:45so that you'll be able to retype the code in your file.
00:48So let's get started.
Collapse this transcript
1. Understanding Particle Systems
Understanding what particles are
00:00A lot of people think of particle systems as something that looks really cool, but not very useful in a real-world application.
00:06Throughout this title I want to put a lot of emphasis on how you can use particles in real-world applications.
00:12In this application, the splashes coming off of the boat are particles.
00:16A particle system is also generated when the fish hits the water and splashes.
00:21The fish itself is even a particle.
00:24So you can use particles in all kinds of different ways in different applications.
00:28You'll notice the background is animating to the left and looping around coming from the right.
00:33In this title we're going to talk about how to do that
00:35using particles.
00:37So as we move forward, don't think the particles are just little dots that you can only use for a small amount of things. We'll talk
00:43about how to use all kinds of different objects as particles and how to use particles in lots of different ways.
Collapse this transcript
Understanding how particle systems work
00:00In this movie, we'll take a look at the general way that a particle system operates.
00:04The first thing we have is a particle class.
00:07The particle classes actually fairly simple. It has several properties that we can modify.
00:12We initialize the properties in the constructor,
00:16and we have an update method
00:17to update all the properties based on some pretty simple math.
00:20Believe it or not this is actually the same particle class that we're going to be using throughout most of this title.
00:25We're just going to add a little bit here and there and use it in different ways to create different types of particles that behave differently.
00:32Animating many particles of the same time is called a particle system and in a particle system, you don't have control
00:38over all the individual particles, but rather
00:41you give each particle a range of values and the whole group of particles behaves a certain way.
00:47For example, I'll test the movie and you'll see the particles behind the boat
00:52shoot out to the left in a little bit. The fish particles come out when the fish makes a splash
00:58and the fish itself as a particle.
01:00And so it jumps up every few seconds and rotates into the direction it's moving.
01:07Let's take a look of the particle code here
01:09for a particle system.
01:10We have some simple variables that dictate how many particles we're going to create, what a class we're going to use to create the particles.
01:17Then we use loops to generate the particles
01:20and to update them by running the update method.
01:23The particles are connected to the particle class
01:26via linkage properties.
01:31So that's a general idea of how a particle system works. We're going to have the same set up with a particle class, a particle system
01:37and movie clips connected to the particle class
01:40through this whole title.
01:41So that's a look at our workflow.
01:43Now let's get started making some particles.
Collapse this transcript
2. Making Particles Move
Building the particle class
00:00In this chapter we're going to build the foundation for our particle class by adding movement to our particles.
00:06If you're following along, you can open up the exercise files folder and in the Chapter 2 folder you'll find the movie
00:12one folder and inside that folder, you'll see the start folder and there's this whole folder structure for our package,
00:18com/lynda/particles and the Particle.AS file.
00:22If you don't have access to exercise files, you can set up the same folder structure and create the Particle.AS file now.
00:28I'm going to be following the same routine through using the particle class file throughout this whole title so I'm just
00:34going to reference the particle class file for that movie in each movie. So let's open Particle.AS and
00:40take a look at how it's set up. We have our package declaration importing the display classes, because we're going to be
00:46extending sprite class. Now the reason we're extending the sprite class is because we want to be able to connect movie clips to our particle class.
00:53So you have to at least extend the sprite class in order to do that. You can also extend movie clip class if you'd like to.
00:59Then we have our constructor method,
01:02and we have method called update. The update method is pretty much where all the magic happens. Basically the way that this
01:08class works is we set up a whole bunch of different properties like velocity and acceleration and we apply to them using
01:15the update method. The way the particles animate is we run the update method repeatedly, over time
01:21from the main Timeline.
01:23So there's a look at our basic particle class. The next thing to do is to start making the particle be able to move by
01:29creating velocity properties.
01:31We'll talk about how to do that in the next movie.
Collapse this transcript
Creating particle velocity
00:00Now we'll take a look at adding velocity properties to our particle class so we can start making the particles move.
00:06If you're following along, I'm working in the particle class for movie two.
00:10So the first thing we'll do is create two public properties, one called xVelocity and one called yVelocity and
00:17they'll both be numbers.
00:19We're give them a value in the constructor function.
00:28So in constructor function, I'm just going to initialize both of these properties to zero.
00:38That way the particles won't move unless we give them X or Y velocities. In the update method,
00:45we'll write the code that moves the particles. All we're going to do is increment their current X and Y positions
00:51by the values of x and yVelocity respectively. We'll take the particle's x-position and we'll add to it in the amount of xVelocity.
01:01So a negative value will move the particle to the left and a positive value of will move the particle to the right.
01:07The same thing for the y-position and yVelocity.
01:10So a positive yVelocity will move the particle down and a negative yVelocity will move the particle up.
01:17And that's really all that there is to velocity. So at this simple level, we simply set a value for the particle's
01:23velcoity and we moved the particle by it's velocity amount.
01:28Now that we have our velocity set up,
01:30we'll connect a movie clip in an FLA file to this class.
Collapse this transcript
Connecting a movie clip to the particle class
00:00Now we'll take a look at connecting a particle movie clip in an FLA file to our particle class. If you're following along,
00:07I'm working in Simple_Particle.FLA and our particle class file. Both of these files can be found in the movie three folder.
00:13If you don't have access to the exercise files, make sure this FLA file is in the same folder as the com folder for your class
00:20and that it's not inside of the class files or not somewhere at some remote location.
00:25You also want have a movie clip in your library called Ball with center registration. So I'm going right-click or Control-click
00:31the Ball movie clip in the library and choose Linkage and I'm going to check Export for ActionScript. Now the
00:37important thing we do here is we connect the particle class to the base class for this symbol and not just the class.
00:44Each item that you export for ActionScript from your library needs to have a unique class name.
00:50So if we were to put our particle class there then we'd only be able to link one movie clip for particle class.
00:56So we'll put in the base class instead,
00:59so that each item we export for ActionScript from our library can potentially be a particle.
01:04So our base class will be com.lynda.particles.Particle- capital P.
01:10Now when you click OK, Flash would tell you that the ball class doesn't already exists so Flash is going to create
01:17a class for you, which is exactly what we want to happen, and that class, ball, is going to inherit from our particle class.
01:24That's exactly what we want to happen.
01:26SO click OK and we've successfully connected our ball in the library to our particle class. Next we'll take a look
01:32at animating particles.
Collapse this transcript
Animating a particle
00:00Now we'll look at making our particle move. If you're following along I'm working in Animating_Particles.FLA in the movie four folder.
00:08On this Stage we have our ball particle and it has an instance name of ball.
00:12Let's go to the first keyframe in the Actions layer and open up the actions panel.
00:16Here I've already written some code.
00:18A function called updateBall that's connected to an interframe event.
00:23So above that,
00:24I'll set some initial properties for the x and yVelocity of the ball. ball.xVelocity and we'll set that
00:30equal to 5 and on the next line, we'll set the yVeolcity of the Ball
00:36equal to negative one.
00:39Then in our updateBall function we'll simply update the ball by calling the ball's update method.
00:46Test the movie and watch the ball animate.
00:52We're seeing that the update method is running every frame, and it's moving the ball based on its x and y velocities.
00:58So now you've seen we created a basic particle animation and it was really simple.
01:02Next we'll take a look at making multiple particles animate at the same time.
Collapse this transcript
Creating a particle system
00:00Now we'll take a look at building a particle system, that is many particles that animate. If you're following along, I'm working
00:06in a Particle_System. FLA. This file can be found in the movie five folder. Notice that there's no ball in the Stage anymore.
00:13There's only one layer as the actions layer.
00:15Let's go the first keyframe in the actions layer and open up the actions panel and here's all the code for this particle system.
00:22Now if you don't have access to the exercise files, you can just copy this code down.
00:26I'll walk you through it quickly and you'll see that is fairly simple. I have a variable here for the total number of particles that I want.
00:32Then I have a variable here that it's an array to hold all the particles and I just initialize it to an empty array.
00:39We add an event listener to the main Timeline to update this Stage in every frame and the updateStage function is where all the
00:45magic happens.
00:46The first thing we do is we check to see if the length of the particles array is less than the number of particles.
00:54If so, then we'll make a particle.
00:56To make a particle we create a new instance of the particle class, which is ball, and we add it to the Stage.
01:02Then we set its x and y velocities to random values.
01:05You can feel free to change the values here to any values that you want.
01:09We place it in an x and y position.
01:12Here, I just put the x and y position at the center of the Stage. Then we add that particle to our particles array.
01:18So this keeps on running every frame until we've reached as many particles as we want in our particles array.
01:24Every frame we update
01:26every particle in the particles array through a loop.
01:29And that's really all there is to it. If you test the movie at this point, you'll see all the particles animate.
01:37You'll notice Flash only creates 20 particles because that's what we have in our numOfParticles variable.
01:43That's really all there is to creating a basic particle system. So at this point you may be wondering how you make
01:48particles continue to come from particle system and to keep animating and to keep creating particles every frame.
01:54We'll take a look at how to do that in the next movie.
Collapse this transcript
Regenerating particles
00:00Now we'll take a look at one way you can regenerate particles to make particles continually flowing from the source.
00:07If you're following along, I'm working in Regenerating_Particles.FLA. This file can be found in the movie six folder. Let's open up the
00:13actions panel and scroll down to the for loop that's inside of the updateStage function.
00:18Now I've added some code here. Because you're probably familiar with how to do these exact techniques, I just want to explain it.
00:24Now what I'm doing is I'm creating a variable called particle to represent particles array index I.
00:30That way I can refer to it shorthand later on. Now I have a conditional statement that may seem massive and amazing complex,
00:37but you're probably familiar with this process. Basically, what we're doing is we're checking to see if the particle
00:42is off the Stage.
00:44We check to see if it's off the Stage to the right
00:47or if it's off the Stage to the left or if it's off the Stage
00:51at the bottom or if it's off the Stage at the top.
00:55So we check the particle's x and y positions and then if the particle is off the Stage,
01:02we reset the particle's velocity,
01:04and then reset the particle's position back to the center of the Stage.
01:08So may look like a fairly large block of code here, but you can see it's pretty simple. We check the particle's
01:14position and then we reset it if it's out of bounds.
01:17So when you test movie you can see that the particles are continually flowing from the center of the Stage, but really what's
01:23happening is that whenever a particle gets out of bounds, it's properties get reset.
01:27And that's just one way to regenerate particles.
01:30You can do just based on the y position or just the x position or you can set up your own boundaries and there
01:36are a few different other ways that I'm going to talk about through the rest of this title.
01:40And you also may also come up with you own. So now that we've created a basic particle system, let's take a look at a particle system in a
01:46real-world application.
Collapse this transcript
In practice pt. 1: Building rain in a weather application
00:00Here is an example of a Flash application that uses the particle system that we've built up to this point.
00:05If you're following along, you can find the file weather.FLA in the movie seven folder.
00:10Before you test the application, make sure Flash is up to date with the latest updates from Adobe. If Flash isn't up to date,
00:17you may get an error when you try to test the movie.
00:19So I'll test the movie and you can see that we're pulling in the city name and
00:24weather information. This is all based on a zip code, this is current data coming from the Yahoo Weather API.
00:31On the left side here, you'll see to this our particle system. It's using the same concepts that we've talked about throughout
00:37this chapter, so we have a y velocity of the particles making them move down and then they're reset and
00:43put it back at the top once they get off the screen.
00:47So there's an example of a particle system in a real-world application.
00:50For those of you who want to stick around, I'll show how this application is set up. First if you'd like to learn more about
00:57developing AIR applications, there's a great lynda title called AIR Essential Training, and there you can learn
01:02all the basics of developing AIR applications.
01:05You can also get a lot of the tools you'll need on the Adobe website.
01:10To find the Adobe AIR site, you can just go to adobe.com/air.
01:15In my browser, it says adobe.com/products/air, but typing in adobe.com/air will get you there as well.
01:22From there you can click the link to download Adobe AIR now.
01:26If you want to get more information about developing applications using Flash, you can go to adobe.com/devnet/air/Flash.
01:35This is the Adobe AIR Developer Center for Flash,
01:38and here you can find a bunch of tutorials and articles about developing AIR applications using Flash.
01:45So let's go back to Flash
01:46and take a look at how this application's set up.
01:49Select the first keyframe of the actions layer and open up the actions panel.
01:53Other than the particle system, this application is primarily XML driven.
01:57If you want more information about working with XML and ActionScript 3.0,
02:01I recorded a title in Online Training Library focused entirely on that topic.
02:06So here we have a URLLoader and some variables set up, one of them that's important is the zip code.
02:13All this weather application is based on zip code. To make it work, I'm using the Yahoo Weather API.
02:19The way that works is you send a URLRequest
02:22to this address that I have highlighted here
02:25and then you tack on whatever zip code you want weather for at the end.
02:29The data that gets loaded in is an RSS feed that has weather information for that zip code.
02:35So in my dataReady function here, I'm processing that weather data and setting variables for the city and
02:41temperature and the weather.
02:43Here for testing purposes, I set the weather equal to rain.
02:48Then I have a conditional statement that checks to see if the string rain is inside of the weather string.
02:54If so, then we have
02:57a movie clip called icon go to and stop in a frame called rain.
03:01Let's take a look at that frame.
03:05The icon movie clip is very simple and it just has two frame labels and two sets of artwork.
03:12One is a sun and it has a sun icon, and one of them's rain and has a cloud and this is where our particle system is contained.
03:19If I double-click the rain movie clip in the library,
03:22I can enter its Timeline.
03:23In this movie clip, I have a cloud and a mask.
03:27The mask is what masks all the rain.
03:30I'll go to the first keyframe of the actions layer and open up the actions panel.
03:34Here you'll see the code is pretty much exactly what we did in the last movie. We're importing the particle class,
03:41we have a numOfParticles variable.
03:43One thing that's new here is that we have a variable that's class datatype that's holding the particle class, which is
03:50raindrop in this case which is going to be our rain particle.
03:53Using this technique is a little bit easier to transport this code into another application later on.
03:59We have an array for our particles and a container to hold them.
04:03At the bottom of the code,
04:05we add the container to the Stage
04:07and put the cloud in front of everything else.
04:10And then run makeParticles every frame
04:13The makeParticles function is pretty similar to the function that we've used before to create particles and to move them.
04:19First, we check to see if the length of the particles array is less than the number of particles that we want.
04:25If so, we create a particle
04:27and we set the particle's properties. Some additional things I'm doing here is I'm setting the mask for the container
04:34and adding the particle to the container.
04:38From there, I have a loop that updates every particle,
04:41checks the particle's boundaries so if it's y position is below the mask,
04:45then I reset the values of the particle.
04:48That is a best practice from working with animated elements in an ActionScript set mask,
04:53I'm resetting the mask here.
04:55Now that we've reviewed the code, let's look to the application again.
05:01So there's our particle system. The rain is simply using y velocity to move down and resetting its position once it
05:08gets out of the range of the mask.
05:10So that's it. Now you can take what you know so far and use it in a real-world application.
Collapse this transcript
3. Adding Realistic Motion to Particles
Creating particle gravity
00:00In this chapter we're going to talk about adding more realistic animation to our particles like gravity and friction and
00:06fading and growing and shrinking and such. We'll start with talking about gravity. If you're following along, I'm working in
00:19Particle_Gravity.FLA and our particle class and both can be found in the Chapter 3, movie one folder.
00:19Let's first go to a particle class file.
00:21The first thing we're going to do is create another public property and we'll call that gravity.
00:27That's going to be a number
00:29and in our constructor we'll initialize that to zero.
00:34Then in our update function,
00:36above everything else,
00:38I'm going to add to the yVelocity in the amount of gravity.
00:43So that way if gravity has a positive value, it will keep adding to the yVelocity value and accelerate the object's movement down.
00:52So I'll add to the yVelocity in that way here.
00:58And that's it. Let's do a File, Save and make sure this file's saved before we give test this out.
01:04And we'll have to go back to Particle_Gravity.FLA and open up the actions panel.
01:09We'll take a quick look at our code and basically this is the exact same code from when we looked at regenerating particles.
01:15So we have a simple particle system that regenerates. If you test the movie, you can see that in action.
01:20And now we're going to apply gravity to particles.
01:23All we're going to do to do that is where we set the x and y velocities for the ball in updateStage in that first
01:30conditional statement, we'll set the gravity for the ball. So ball.gravity and we'll set that equal to one.
01:38Then we'll test the movie and see that in action.
01:43So as the balls come out from the center of the Stage,
01:46you can see that they fall, there's sort of a gravity pushing on them.
01:51We can weaken the gravity by reducing that amount so let's say .5 for the gravity.
01:59And they gradually fall down or let's say we made the gravity a negative value.
02:07And they all gradually move up.
02:12I'm going to change the value back to one,
02:15and that's how to apply gravity to particles.
Collapse this transcript
Creating particle friction
00:00Along the same lines as gravity, another thing that can make particle movement a little bit more realistic is friction.
00:06In this movie, we'll take a look at adding friction to our particle system.
00:09If you're following along, I'm working on our particle class file and Particle_Friction.FLA and both files will be found in
00:15the movie three folder.
00:17Both files will be found in the movie two folder.
00:20So in this file we'll create a public property and we'll call it friction. It's going to be a number and
00:27we'll initialize it in our constructor to one.
00:31What we're going to do with friction is we're going to mulitply it by the current values of the x and y velocity properties.
00:39Let's scroll down to our update method and at the very top of our update method,
00:43we'll apply friction into our x velocity property.
00:49So we're going to multiply x velocity
00:51by friction.
00:54Now with a value of one, friction isn't going to change at all, but let's say the value were something like .9.
01:01Then every time we update our particle, friction will reduce the xVelocity ever so slightly and this will kind of apply like
01:08an easing effect and make the motion a little more realistic.
01:11So we'll apply the same thing to yVelocity, multiply it by friction.
01:17And that's really all there is to applying friction. Other than setting up the value in our FLA file, that's a pretty simple
01:23process. So let's save this file.
01:26File, Save and then head over to Particle_Friction.FLA.
01:30In the code in that file,
01:31right below where we set our gravity for the ball, let's set the ball's friction. Now I'm doing this in the initial conditional
01:39statement because we only need to set the friction for the ball one time.
01:43And I'm going to set this equal to .98
01:48and to make it a little the more obvious with the gravity applied, I'm going to say gravity of .5 so I'll weaken the
01:54gravity a little bit and I'll apply some friction and you'll see the particles slow down horizontally and vertically.
02:01So when we test the movie, because of friction, the particles slow down horizontally and vertically but because of gravity,
02:08the particles speed up vertically a little bit.
02:16So now our motion's a little bit more realistic.
02:18Now, if you wanted the particles to speed up a little bit with friction you could apply kind of a negative
02:24value in gravity but with friction it would just be a value over one. So let's say I did 1.05, for example. Now when I test the movie
02:32we'll see when the particles update,
02:35the x and y velocities are increasing.
02:42And that's all there is to it. So now you can apply friction to your particles as well.
Collapse this transcript
Growing and shrinking particles
00:00Another effect we can add to our particles is to make them grow or shrink. If you're following along, I'm working in Particle.As
00:07and Particle_Grow.FLA. Both of those files are in the movie three folder.
00:11We're going to make our particles grow in a similar way to how we applied friction to our particles.
00:15We're going to create growX and growY properties. They're both going to be numbers.
00:24Make sure to specify them as public as well.
00:30Then we'll initialize them with values of one.
00:33So growX equals one
00:36and the same thing for growY.
00:39Then at the very bottom of our update method, we'll modify the scaleX and scaleY properties by their growX and growY
00:49properties respectively. So scaleX times equals growX and the same thing for scaleY.
00:56And we'll multiply scaleY by growY.
01:01So as the particles shoot out, they'll scale up.
01:04Let's save this file, File, Save and then jump over to our FLA file.
01:10Right below where we set the initial X and Y positions of the particles I'm going to set
01:15growX and growY properties.
01:18Set growX equal to 1.02
01:23and then we'll set growY equal to 1.02 as well.
01:28And when we test the movie,
01:31we'll see they grow then after they-
01:34(Laughs.) Ah, that's really funny.
01:39You'll see the particles continue to grow after they regenerate so we kind of want to reset the X and Y scales
01:45of the particles once we regenerate them. So I'm going to scroll down
01:49and we're in an area where we reset the particles in the conditional statement, it's within the loop in our code.
01:55We'll just set of particle.scaleX equal to particle.scaleY
02:03and that will be one.
02:06So we'll set both of them in the same line of code there. That's fine to do.
02:09Test the movie
02:11and the particles grow,
02:14and they shrink back to their original size once they're recreated.
02:18And you can see that the particles shrink back to their original size when we reset them,
02:23and that's all at there is to making particles grow.
Collapse this transcript
Fading particles
00:00Now we'll make our particles fade out.
00:02We'll do that in the same way that we applied friction and grow to our particles.
00:06If you're following along, I'm working in the Particle class and Particle_Fade.FLA and both files can be found in the
00:12movie four folder.
00:14So let's create a public property and we're going to call that fade.
00:22We'll set the data type as a number and then initialize the value at the bottom of our constructor to one.
00:29And the what we'll do is at the bottom of our update method, we'll modify the alpha of the particle by multiplying that by fade.
00:38So if the value is a little bit less that one and then the object would fade out a little bit every time we run the update method.
00:45That's also where we're going to add to our particle class so let's choose File, Save,
00:49save this file, jump over to Particle_Fade.FLA, open up the actions panel and I'm going to scroll up in my code.
00:57I'm going to find were we said the initial properties for the ball.
01:01And then right below where we set growX and growY
01:05we'll set fade for the ball
01:08equal to .98.
01:11Now we test the movie.
01:14And you'll see the particles fade out.
01:19Notice that they can become completely invisible after a while so obviously we'll have to reset the alpha for the particles.
01:25So we'll do that
01:26at the bottom of our conditional statement inside the loop
01:29in the updateStage function.
01:32We'll set the alpha of our particle, for each particle that gets reset, to one
01:38and one thing we can do
01:39is we can check to see if the particle's alpha is less than or equal to zero.
01:44If so, we can reset that particle.
01:46So I'm going to add that to my already large conditional statement. I'll add an OR operator in there and we'll just check to see if the particle's
01:53alpha property is less than or equal to zero.
01:56That will just be one more expression is check to see when we reset the particle. So we'll test movie now
02:04and the particles fade out a little bit and they regenerate and start again in an alpha of one.
02:11So that's how to fade particles. Now that we've taken a look at adding some more realistic animation effects to our particles,
02:18let's take a look in using these effects in a real-world application.
Collapse this transcript
Creating particle rotation
00:00In this movie we'll take a look at adding rotation of particles so that they rotate in the direction that they're moving.
00:06If you're following along, I'm working in Particle.AS and Particle_Rotation.FLA and both files can be found in
00:11the movie five folder.
00:13If you have these files, the first thing I want you to do is test the movie.
00:16I built a particle system here that has arrows and I took away their gravity and friction properties so we can really see
00:23this in action. So test the movie right now.
00:26You can text it straight from Particle.AS.
00:30And here you can see arrows and notice that they're all pointing to the right, but they're all moving in different directions.
00:36So our goal here is to make the arrows just rotate or point towards the direction that they're going.
00:43So the first thing we'll do here is we'll create a public property and we'll call that autoRotate.
00:50It's going to be Boolean and we'll initialize it to false in our constructor.
01:00Then in our update method we'll check to see if the value of autoRotate is true.
01:06If so, then we'll rotate the particle based on it's X and Y velocity. In order to move a particle based on it's X and
01:13Y velocities, we calculate the ratio of it's X and Y velocities and get an angle out of that.
01:20In order to do that we're going to use the built-in Math method called Math.atan2.
01:25Math.atan2 takes in two properties. First it takes a vertical value, so we'll pass in yVelocity first and
01:32then it takes a horizontal value so we'll pass in xVelocity second.
01:36I'll press F4 on my keyboard to get a little more space for writing my code.
01:40What you really need to remember where you using Math.atan2 is to pass in yVelocity first, because you naturally want
01:47to pass in xVelocity first because you typically think X and then Y.
01:51But in this method it's reversed.
01:53That's how they get you. So remember that and then that value that's returned by Math.atan2 gives us radians and that's
02:01not an angle that we can use to rotate an object. In order to rotate an object, we need to convert those radians to an angle.
02:09The formula to convert radians to an angle is to multiply the radians by 180 over Pi.
02:15So we'll take our Math.atan2 and multiply that by 180 divided by Math.PI.
02:22So that's all there is to it. So let's choose File, Save and we'll jump over to Particle_Rotation.FLA and open up the actions panel.
02:32You'll notice that the code for this particle system is a little bit different than what we've done before.
02:36We have our numOfParticles in our particle class that represents our library items so we can make easily
02:42transportable code.
02:43We have a particles array, a container to hold them and then we have a Timer. So we're running our makeParticles function
02:49based on a Timer and not just based on ENTER_FRAME event.
02:55Let's take a look at the run immediately code.
02:58We're putting the container on the Stage, adding an event listener to the timer to run make particles.
03:03Starting the timer and then we're listening for ENTER_FRAME on the main Timeline and running updateParticles every frame.
03:09That function is very simple. It just updates every particle in the particles array.
03:14Where the real magic happens is in the makeParticles function. Let's go there and the first thing we do is we clear the container.
03:21So anything that's inside of our container we get rid of it
03:24using both removeChild and the shift method of the array class to take away the first particle from the particles array.
03:30The next thing this function does is it has a loop to create particles so along with a Timer, we're creating all the particles that
03:36we want to create immediately, so we kind of get this explosion effect. So what we have to do in here
03:41is set the particles autoRotate property to true.
03:47Then you'll see the particles rotate
03:51along with their X and Y velocities.
03:54Notice they're appearing wherever the mouse is.
03:57Now they're rotating based on the direction that they're moving.
04:01So that's a success.
04:03The next thing we want to do is apply gravity and watch them rotate down as they move down.
04:08I have that in my code, and it's commented so I'm going to uncomment that now and show it.
04:14Notice as the particles move down they rotate downward.
04:20So that works great.
04:22Looking at the way these arrows move, reminds me of something else. So what we're going to do now is uncomment this code
04:28and we're going to change those arrows into fireworks.
04:33Notice I'm simply uncommenting code that sets fade values, growX and growY.
04:39I'm applying a colorTransform to each particle.
04:45Making sure to import the colorTransform classes,
04:48and I'm going to change the particle class to firework
04:55and inside of the firework particle,
04:58there's another movie clip called glow_mc, and that's where we're applying the colorTransform.
05:03Now I'm just going change the background to black and I'm going to need to press F4
05:08to get all the panels back. I'll change the background to black and test the movie
05:13and behold the fireworks.
05:22And there you have it. So there's how to rotate particles in a particle system based on the direction that they're moving.
Collapse this transcript
In practice pt. 2: Creating boat spray
00:00Here's another example of an application that uses the particle system techniques that we talked about throughout this chapter.
00:05If you're following along, I'm working in boat.FLA and that file can be found in the movie six folder.
00:11So let's test the movie and walk through some of the features of this application.
00:15It is just a simple animation of a boat
00:17and the boat's shooting out some splash particles that fade and grow as you can see. Another particle in this application is the fish.
00:26The fish jumps out of the water and
00:28using the autoRotate feature from the last movie,
00:31it rotates in the direction that it's moving.
00:33Once the fish gets back into the water some splash particles shoot up.
00:37So there's another example of how you can add particles to one of your applications. For those of you who'd like to stick around,
00:43I'm going to walk through how this application is set up.
00:46It's pretty simple on the surface, with several different layers that are kind of self explanatory, like a background and the
00:51moving background and the background waves.
00:55There's a mask that is masking the fish and the silhouette of the boat
00:59and the actions layer and that's pretty much it.
01:02So I'll select the first keyframe of the actions layer and open up the actions panel.
01:06And what we're doing here is we're importing the particle class.
01:10We have a number of particles, the particle class variable to represent the particles that are going to be coming out of the boat,
01:16that's called splash, an array for the boat splash particles,
01:21a container to hold the boat splash particles, a Timer for the fish to jump out-
01:26that's going to run every three seconds-
01:28a variable to hold the initial Y position of the fish,
01:32a variable to hold the fish splash particles and a Boolean variable
01:37to make sure that we don't continuously create particles as the fish is jumping.
01:42Let's scroll down and see the code that runs immediately.
01:46So we're adding some elements to the Stage.
01:48Adding an event listener to the main Timeline to listen for the ENTER_FRAME event and to run the makeParticles function.
01:54Adding an event listener to the Timer, listening for TimerEVENT.TIMER and running a function called moveFIsh. Then we start
02:01the timer, so let's first take a look at the function that makes the particles. It's called makeParticles so I'll scroll up
02:05and find makeParticles and what happens first is we create a variable called particle, we create a new instance of
02:11the particle class with that variable
02:14and set some initial properties, add it to the Stage and run a function called makeParticle
02:20passing in the particle that we're making.
02:22The makeParticle function, if you scroll down, simply sets initial values for each particle. Values include X and Y, x- and yVelcocity
02:30alpha, scaleX and scaleY, then we put that particle in the particles array.
02:36Going back to the makeParticles function,
02:38if we ever have more particles in our particles array than the number of particles that we want total,
02:44we first remove that particle from the particles array then we remove it from its parent container.
02:49Then we simply have a loop that runs through each of the particles in the particles array and updates them all.
02:55Inside of here, we also run a function called updateFish, which simply updates the fish on the Stage and the fish splash
03:03particles. Let's take a look at that function.
03:06Scrolling down to updateFish, now this is different from the function that runs on the timer, which is called moveFish.
03:12So keep that in mind as we look through this. The first thing we do in updateFish is we check to see where the fish is on the Stage.
03:18If it's low enough to make a splash, then we make the splash.
03:23That's what this loop does here. First we check to see if this is a new jump.
03:28Then we said the X and Y positions of the splash particles for the fish at the same location of where the fish is and
03:34then we update
03:36each particle in the fishSplash array. After the loop we set new jump to false so we're not continuously moving fish splash
03:43particles to the position of the fish as this function is running and then we update the fish.
03:49Let's take a look at the moveFish function, which makes the fish shoot out of the water.
03:53We have a variable called splash.
03:56We're setting newJump to true,
03:58setting the initial X and Y properties for the fish, x- and yVelocity, gravity, friction, autoRotate. Then we empty out
04:07the fishSplash array and make sure to remove all the child elements from the Stage.
04:14And then what this loop does right here
04:16is it creates a whole bunch of fish splash particles, kind of like what we did with the timer and the fireworks.
04:22So it creates all the particles at once.
04:25So we have a new FishSplash,
04:27set fade, growX, growY,
04:30and the friction, all the properties that are going to be the same for each splash particle.
04:34Add them all to the Stage,
04:36put them in the fishSplash array and then run makeFishSplash on each of the particles.
04:41makeFishSplash sort of initializes each particle in the particle system.
04:47We set the Y position equal to the fishY,
04:49set x- and yVelocity.
04:51The yVelocity's based on the velocity of the fish and the xVelocity is a random value.
04:56We set gravity to a random value
04:59and initialize alpha, scaleX and scaleY to one,
05:02and that's really all there is to it.
05:04You can see that the application gets a little bit complicated because you're using different particle systems at the same time,
05:10but I'll test it again and we'll once again review how the particle systems are working in this application. So we have the boat
05:16splash particles that are fading and growing,
05:20effected by gravity and friction.
05:22The fish is a particle that auto-rotates and the fish splash particles
05:27shoot up once the fish hits the water.
05:29Now this point, you may be noticing that the animation is slowing down
05:33or for some reason it's not playing up to the speed that it should or playing along with a frame rate and that's because
05:39we haven't yet optimized this application. We'll take a look at how to do that in the next chapter.
05:44But for now you have another example of using particles in actual application.
Collapse this transcript
4. Optimizing a Particle System
Understanding garbage collection
00:00Ween you're working with something with a lot of complex animation like a particle system it's really important to keep your files optimized.
00:06In this chapter we're going to talk about two ways you can optimize your particle systems.
00:11The first thing we're going to talk about a something called garbage collection.
00:14Garbage collection refers to the Flash Player discarding unused objects.
00:19In order for an object to be eligible for garbage collection, it can't be in any array, it can't be on the Stage or in any
00:25other display object and it can't have any other references to it, like event listeners. So you have to manually remove any event listeners
00:31that are attached to an object to make it eligible for garbage collection.
00:34In a Flash application like this, garbage collection is not controlled by you the Flash developer. The way that garbage
00:41collection works is that as memory usage gets to a certain point, Flash does something called a mark and sweep.
00:47The Flash Player sort of scans your application looking for elements that are eligible for garbage collection and once
00:54it identifies all those elements, it gets rid of them through a process called a sweep.
00:59So it's important for you to note when you're developing applications that simply removing an object from the Stage or
01:04taking it out of an array doesn't automatically mean it gets garbage collected immediately.
01:09Garbage collection is something that happens sporadically and you're actually not guaranteed when or if it will ever happen.
01:16Regardless, you want to keep any unused objects eligible for garbage collections so that you can keep your file optimized.
01:23If you're following along, I'm working Garbage_Collection.FLA, which can be found in the Chapter 4, movie one folder.
01:29Let's open the first keyframe of the actions layer and open up the actions panel.
01:33This is the same boat application that we looked at in the last movie of the last chapter.
01:39The only thing I changed is the number of particles, I changed it to 100.
01:43Let's take a look at what I did in this application to optimize it for garbage collection.
01:48There's a while loop inside of the makeParticles function
01:52that runs as long as the length of the particles array
01:55is greater than the total number of particles that we want.
01:58As long as that's the case, we remove the first particle in the particles array and we hold it in the particle variable.
02:05So that's the first step, we take it away from the array. The second step is to remove it from the Stage.
02:09That's what we're doing here using removeChild,
02:12and now I know there aren't any other references to this object later on, but just to be safe I'm setting the particle's value to null.
02:19The reason I set the object value to null is so later on I get an Output window in case I forgot to remove any references to the object later on.
02:27The other place where I optimize the application for garbage collection is toward the bottom of the code.
02:32This is in the moveFish function.
02:34What I did is I emptied out the fishSplash array
02:38using a while loop.
02:39I hold the fish splash particle in a variable
02:42and remove that from the beginning of the fishSplash array, remove it from the Stage and set it to null.
02:48So that's essentially how you optimize an application for garbage collection. Take the element out of any arrays, remove it from
02:55the Stage, remove any event listeners or any other references to that object
03:00and then garbage collection sort of happens on its own.
03:03So I'll test the movie
03:06and we'll notice that it runs smoothly,
03:07with 100 particles in it.
03:09That's 100 particles for the boat and for the fish splash.
03:13So that's one way to optimize your Flash movie.
03:16So whenever you have unused elements in a Flash application, make sure to make them eligible for garbage collection.
Collapse this transcript
Recycling particles
00:00Another way to optimize a particle system is to only create as many particles as you need and then to simply recycle those particles.
00:07If you're following along, I'm working in Recycling.FLA and this file can be found in the movie two folder.
00:12Let's go to the first keyframe of actions layer and open up the actions panel.
00:16If you scroll down to the makeParticles function you'll see that I'm only creating particles if the length for the particles array
00:22is less than the total particles that we want.
00:26In the loop, I'm updating all the particles and if a particle is ready to discard,
00:31I simply run the makeParticle function on that particle,
00:34which simply initializes the particle.
00:37If you scroll down to the moveFish function, you'll see I did the same thing with the fish splash particles.
00:43I only create them while the length of the fishSplash array is less than the total particles that we want.
00:49And then I have a loop to initialize them using the makeFishSplash function.
00:54That's really all there is to recycling. Instead of discarding them, you just re-initialize them.
00:59Let's test the movie and see what it looks like.
01:02So here's this example of our recycling technique.
01:08And it may look pretty close to the garbage collection technique and there might not be any difference at all on your machine.
01:14So at this point, you may be wondering which method you should use.
01:18The answer really depends on your application.
01:21So you'll have to test it using both methods and you can pick which one works better and you can also test using different
01:26amounts of particles.
01:27If you want to, you can also check out how the particle system works on different machines.
01:31One thing I recommend that you don't do is use as many particles as your machine can possibly handle.
01:37What I recommend doing is using as few particles as you can because particle systems tend to be pretty intense on the
01:42Flash player.
01:44So there's look at how to optimize your particle systems.
01:47So make sure to go to your particle systems and make sure you're following at least one of these techniques to keep them optimized.
Collapse this transcript
5. Adding Collision Detection to Particles
Using position-based collision detection
00:00In this chapter we're going to be talking about collision detection, that is making particles interact with other elements.
00:06The first kind of the collision detection we're going to talk about is position based collision detection.
00:12If you're following along, I'm working in Basketball.FLA
00:15in the Chapter 5, movie one folder.
00:18If you don't have access to the exercise files you can just create a simple basketball movie clip,
00:23or any ball for that matter, and connect it to the particle class. Let's go to the first keyframe of the actions layer and open up
00:29the actions panel.
00:30The code I have written here is really simple. We just have an event listener for ENTER_FRAME running moveBall, which simply updates
00:37the ball every frame.
00:38If you test the movie now, you'll see that the ball doesn't move because there's not yet a gravity, friction or velocity applied.
00:45So what we'll do is, above this function declaration, we'll initialize the gravity for the ball to one.
00:55Now when we test the movie,
00:57we'll see the ball moves down
00:58according to gravity and then in the moveBall function right below where we run the update method, let's do a
01:04conditional statement to check the position of the ball. If the position of the ball is at the bottom of the Stage,
01:10we'll make the ball bounce up. So let's check the Y position of the ball and see if it's greater than stage.stageHeight,
01:19minus the height of the ball divided by two, because the ball has center registration.
01:26And if so, I'm going to copy the code in the conditional statement here and paste it in the curly braces and set that > to an =.
01:37If it's past the bottom the Stage, we'll said the ball right on the bottom the station and we'll reflect its yVelocity.
01:44The way that we do that is we multiply its yVelocity by negative one.
01:51Let's test the movie and see how this works.
01:54So when the ball hits the bottom of the Stage, it bounces right back up.
01:58Notice that gravity is affecting the ball,
02:00but it's not bouncing less over time. That's because we also have to apply friction.
02:06So on the next line of code, right below where we set the gravity for the ball, I'm going to set the friction for the ball to .98.
02:17Test the movie. The ball bounces less and less each time.
02:22Until it pretty much stops.
02:26So now we have some initial effects of the ball interacting with its environment. We're taking the position of the ball,
02:31and reflecting its yVelocity based on its position and don't think you're limited to only yVelocity here. Of course, we could
02:37reflect it off the right side of the Stage or off of any other object.
02:41We simply check its position and reflect it based on its position.
02:45So that's how to control collision detections based on a particle's position.
Collapse this transcript
Using built-in collision detection methods
00:00Flash actually has some built-in collision detection methods that we can use to detect collisions between objects and points.
00:07In this movie, we'll take a look at those methods.
00:09If you're following along, I'm working in Hoop.FLA and you can find this file in the movie two folder.
00:14If you don't have access to the exercise files,
00:16you can just make a movie clip of a basketball hoop
00:19and call it hoop_mc and put a rim in it called rim_mc.
00:24Let's go to the first keyframe of the actions layer and open up the action panel to see what code is in there.
00:30Capturing the initial X and Y position of the ball,
00:34adding an event listener to the ball for a mouse click, setting its button mode to true.
00:39When we click the ball,
00:41we set its initial gravity, friction and x- and yVelocity properties.
00:45Then we add an event listener to the Stage to move the ball.
00:50The moveBall function updates the ball, checks to see if the ball
00:55is out of bounds of the Stage's height.
00:58If so, it resets the X and Y position of the ball
01:01and then takes the event listener away from Stage. So if you test the movie and you click on the ball,
01:06see that the ball shoots toward the basket, doesn't appear to go in the basket and then appears back at its original spot after it
01:12goes off the Stage.
01:14So our goal is to connect the ball and the rim.
01:18So the bottom of this moveBall function, I'll create a conditional statement and we'll check to see if the ball is touching the rim.
01:26ball_RC.hitTestObject. This is the first built-in method to test to see if one object is touching another object
01:33and we'll check against hoop_mc.rim_mc.
01:41And if the ball is touching the rim, we'll set its X position to the same X position as the rim.
01:50So to get that a value, we'll have to take the X position of the hoop
01:55and add on the X position of the rim inside of the hoop.
02:01What you're also going to want to do is set the xVelocity of the ball to zero so it's doesn't keep moving to the right once it's connected
02:08with the rim.
02:14Test the movie, click on the ball
02:18and it appears to align with a rim once it hits.
02:21See how there's kind of a jittery motion? That's because we're using hitTestObject. So as soon as any part of the ball's bounding
02:28box touches any part of the rim's bounding box
02:31then we automatically align so that's how hitTestObject works. Typically hitTestObject is really good for rectangular objects.
02:39For any objects that are circular,
02:41you want to look at other collision detection methods. Another built-in method that we can use is called hitTestPoint.
02:49The great thing about hitTestPoint is that we can use the ball's round shape
02:55as the hit test area.
02:57What you need to pass in are two values, the X and Y position of the point that we're checking against,
03:03and then there is an optional parameter called shape flag that checks to see if we want to check against the bounding
03:09box of the ball or the ball's actual pixel boundaries. By setting that value to true,
03:15we can check against the actual round part of the ball and not just the bounding box.
03:20So the first value we'll pass in the hitTestPoint is hoop_mc.rim_mc.x,
03:25then we'll add-on the X position of the hoop.
03:29And then the next parameter,
03:32we'll take the rim's Y position
03:37and add on the Y position of the hoop.
03:41For the next parameter, we'll just pass in true. And we'll test the movie now
03:46and it should be a little bit less jerky as it works.
03:51So now you'll notice that the ball doesn't shift in; it more slides right into the hoop and it looks a little bit cleaner.
03:57So those of the built-in methods that are used to detect collisions in Flash.
Collapse this transcript
6. Exploding Particles
Adding particle spin
00:00In this chapter, we're going to take a look at making particles explode.
00:03The first step in making particles explode is adding a spin property to our particle class.
00:09That's what we're going to do in this movie.
00:10If you're following along, I'm working in Spin.FLA and Particle.AS and both files can be found in the Chapter 6, movie one folder.
00:17Let's go to Particle.AS first.
00:19In this file, we'll add another public property called spin,
00:24and it's a Number and we'll initialize it to zero
00:29in our constructor.
00:32At the bottom of our update method
00:33we'll update the particle's rotation by the spin amount.
00:40That's it. Make sure to save this file so choose File, Save. Let's jump over to Spin.FLA.
00:45If you don't have access to the exercise files, you can simply create a movie clip and connect it to the particle class.
00:51Let's go to the the first keyframe of the actions layer and open up the actions panel. Here we have a pretty simple particle system.
00:57I'm only creating one particle at the moment, particle class is Star, I've got our particles array container to hold them and
01:03I'm using the recycling technique to create and to update the particles. So if we test the movie,
01:10you'll see right now the particle has no velocity or acceleration.
01:13So what we're going to do is at some spin on the particle
01:17and see what it looks like. I'm going to do this in our IF statement, near the top of the makeParticles function.
01:25Let's set it equal to five.
01:28Test the movie
01:30and watch the particle spin.
01:34Now we can add this to our entire particle system along with gravity and friction if we simply uncomment the code that
01:40I have commented here.
01:43I'll uncomment the x- and yVelocity lines of code here.
01:46And then I'll add some more particles to the particle system,
01:49let's try something like 50,
01:51and then test the movie.
01:56And there you can see our spinning particles.
Collapse this transcript
Making child objects explode
00:00Now we'll take the spin technique that we talked about in the last movie and apply it to all the elements inside of any movie clip
00:07and make them explode when you click on that movie clip.
00:10To do that, we're going to have to modify our original particle class a little bit.
00:14If you're following along, I'm working in Explode.FLA and ParticleChild.AS. Both files can be found in the movie two folder.
00:20Let's jump over to ParticleChild.AS.
00:23You'll see here this file's pretty much exactly as same as our particle class except I changed the name.
00:29So now it's ParticleChild instead of particle.
00:32This needs to be able to work with any object inside of a movie clip and the way it's going to work is we can take
00:37any object inside of a movie clip, pass that object into our constructor function and make a particle out of that object.
00:44So instead of the extending the Sprite class, we're not going to extend any class.
00:48We're also going to create a public property called object. That way we can hold the DisplayObject to reference
00:55and modify it in this class.
00:58So the data type will be DisplayObject.
01:01We'll accept one parameter in the constructor,
01:03going to call that a OBJ, short for object, and that's going to be a DisplayObject as well and for the first line of the constructor
01:10we'll set the value of our object property to the value of the object passed into the constructor.
01:16Then instead of applying all of the transformations to the instance of this class, we're going to apply the transformations
01:22to the object passed in via the constructor.
01:24So I'm going to highlight this
01:26and press Command+F or Control+F to open up the Find and Replace window and replace this with object.
01:33Going to click Replace All,
01:34click OK and then close the Find and Replace window.
01:37Choose File, Save to save the file and then go back to Explode.FLA.
01:42On the Stage here, I have a simple stick figure movie clip that's made up of a circle and a bunch of copies of a line
01:47movie clip.
01:48If you don't have access to the exercise files, don't worry about exporting these elements for ActionScript.
01:53When we're working with the ParticleChild class, you won't have to do that. Let's select the first keyframe of the actions layer and
01:58open the actions panel.
02:00Here we have a variable called particle that's going to be an instance of the ParticleChild class.
02:06At the bottom of the code we added an event listener to the stick figure on the Stage,
02:09listening for a mouse click, and it's going to run a function called explodeObject.
02:14The explodeObject function captures
02:17the stick figure that was clicked on in a variable
02:20and then runs a loop.
02:22For every child in the movie clip,
02:24we create a new instance of the ParticleChild class, passing in each child.
02:30Set some properties for that particle
02:32and put in the particles array.
02:34Then we add our event listener to animate the particles, which simply updates all of the particles.
02:39Let's test the movie and see this in action.
02:42Click on the stick figure on the Stage and watch it explode.
02:47Close the preview window now. Let's go back to the Stage and watch how easy it is to explode any other object.
02:53I'll drag another instance of our stick figure from the library onto the Stage.
03:00Call it stick2_mc
03:04and then I'll drag an instance of a movie clip I created called text
03:07from the library onto the Stage.
03:09This movie clip is a static text field that I broke apart so that each letter is an individual text field.
03:14I'll give this an instance name of text_mc.
03:18And I'll go back to my code.
03:20Copy the addEventListener line of code at the bottom.
03:24We'll add an event listener to stick2_mc and text_mc.
03:29Test the movie. Just click on the movies that we attached those event listeners to and they explode.
Collapse this transcript
In practice pt. 3: Exploding superheroes
00:00Here's an example of an application uses the exploding techniques that we talked about in this chapter. If you're following along,
00:06I'm looking at Flying.FLA in the Chapter 6, movie three folder.
00:11Let's test the movie and see what this application looks like.
00:14There's some clouds in the background that wrap around and animate back to the right after they go to the left edge of the Stage.
00:20And there's a flying guy on the right side of the Stage. If you click the flying guy, he explodes.
00:26Notice all the particles fly to the left because they're affected by the wind.
00:31For those of you who would like to stick around, I'll walk you through this application.
00:34Let's go to the first keyframe of the actions layer and open up the actions panel.
00:38This explodeObjects function may look very familiar to you. The biggest change that I made is that I modified
00:43the xVelocity of the particles so they mostly shoot to the left.
00:48If you scroll down, you see the animateParticles function
00:51and the initial properties that I set for the clouds.
00:56And I added the event listener to the Stage to animate the particles initially.
01:00Then I added an event listener to the flying movie clip, which is inside of the animated movie clip,
01:06which simply makes the flying superhero move up and down.
01:09Let's take a look at the animateParticles function. It updates each particle
01:13and it updates each cloud
01:15and runs a function called checkPosition on each cloud.
01:19If the cloud particle is passed to the left edge of the Stage,
01:22and a little bit more, which is randomly decided,
01:24that we reset the particle's X position
01:27past the right edge of the Stage a little bit.
01:29and we reset the particle's Y
01:31and scale X and scale Y properties, also the particle's xVelocity and alpha.
01:37That's really all there is to this application.
01:40If we test it again,
01:41see the superhero flying.
01:44Click on him to see him affected by the wind,
01:46and all the particles explode to the left.
Collapse this transcript
7. Creating a Smoke Particle System
Using timeline-animated particles
00:00Now we'll talk about how to build a smoke particle system.
00:03If you're following along, I'm working in Smoke.FLA and AnimatedParticle.AS and both files can be found in the
00:10Chapter 7, movie one folder. The first secret to making smoke is building the particles correctly.
00:15I'm going to open up the Smoke movie clip in the library.
00:18This is actually a really simple animation of a graphic symbol that has sort of a moon shape
00:24and it grows over 10 frames
00:27then it spins in a circle until Frame 50.
00:29That's really all there is to it. I'm just having a movie clip that's animated as my particle.
00:33Because I did that I created a new class called AnimatedParticle. AnimatedParticle is the same thing as the particle class
00:40except it extends the movie clip class instead of the Sprite class.
00:45If we try to create this smoke particle using the Sprite class,
00:49or a class that extends the Sprite class,
00:52then we'd only see frame one of this movie clip.
00:55And if we extend the movie clip class for every particle that we created, then we might use more memory than we need.
01:01That's why I created a separate class for this.
01:03So I'm going to go back to Smoke.FLA.
01:07So to scene one,
01:08select the first keyframe of the actions layer and open up the actions panel.
01:12You'll see that this particle system is pretty similar to every other particle system that we've worked with.
01:17I'm using the garbage collection method to dispose of unused particles,
01:21updating all the particles in the array every frame
01:24and discarding particles if their alpha gets too low.
01:27One of the big secrets to creating smoke particles is the properties you set for the particles.
01:32For example, I give my particles gravity a value of -. 1
01:37so they gradually move up.
01:39For the friction of the smoke particles, I put it a value above one so they gradually speed up as they animate.
01:46I gave them random variable low yVelocity values.
01:49and xVelocity values.
01:51Some fade.
01:53Random alpha,
01:54random scale,
01:57made them grow a little bit
01:58and that's about it.
01:59If I test the movie,
02:02you'll see that it doesn't look too much like smoke.
02:05Kind of behaves like smoke,
02:06but doesn't exactly like smoke.
02:09All that we're missing
02:10is applying a Blur filter. So I'll close the preview window and scroll down and I'm going to apply the Blur filter to the
02:16container that's holding all the particles.
02:23I'm going to do this in shorthand notation.
02:26Inside the square brackets, I'm going to create a new instance of the BlurFilter class
02:32and I'm going to pass in a few values.
02:33I'll pass in 200,
02:35another 20 for blurX for blurY respectively, and one, that's for the quality.
02:41If you want to optimize your filters then you can give them even numbers for blurX and blurY.
02:46You might want to take a note here that filters in Flash are optimized for even numbers.
02:51I'm going to keep the quality low because the higher the quality, the more memory I'm going to use.
02:55So I'll test the movie at this point
02:58and check out the smoke.
02:59So there's how to create a smoke particle system.
Collapse this transcript
In practice pt. 4: Making a boat break down
00:00Here's an example of an application that uses the smoke particle system that we created.
00:04If you're following along, you can open up Boat.FLA in the movie two folder.
00:08This file's an animation of a boat breaking down,
00:11and a bunch of smoke coming out of it.
00:15So there's an example of using smoke in an actual application. For those of you who'd like to stick around, I'll walk you through the code.
00:21Let's go to the first keyframe of the actions layer and open up the actions panel.
00:24The code in this application is pretty much the same code as was in the last movie.
00:28I simply modified the X position of the smoke to be a random value based on the width of the boat
00:33and the height of the boat.
00:36I used the same logic for the gravity and friction of the boat
00:39and for the fade, alpha and all the other properties.
00:42I used the garbage collection optimization model.
00:46And I applied a filter to the smoke container.
00:49That's all there is to it.
00:51Let's take a look at it again.
00:54And there goes the boat.
Collapse this transcript
8. Integrating a Particle System with Video
Accessing pixel color
00:00In this chapter, we'll add a particle system to Flash Video,
00:04by getting the pixel information for the video as it plays then making particles appear based on the colors in the video.
00:10In order to do that we're going to need to be able to isolate the red, green and blue values of a color.
00:17In this movie, we'll talk about how to do that. If you're following along, I'm working in Understanding_Color.FLA
00:22in the Chapter 8, movie one folder.
00:25On the Stage, I have a very simple movie clip called color_MC
00:29and it just has a bunch of squares that are red, green, blue, black and white.
00:33Our goal is to get the pixel information for these squares
00:37and isolate the color values for each one. Let's go to the first keyframe of the actions layer and open up the actions panel.
00:44Here I have some code creating a new instance of the BitmapData class,
00:48event listener to run showColor every frame,
00:51and a showColor function that has some variables defined
00:56and a trace statement.
00:57Creating a new instance of the BitmapData class will allow us to get pixel information for a display object.
01:03When you initially create a BitmapData object, you have to pass in two parameters,
01:07the width of the object and the height of the object.
01:10I'm going to pass in color_mc.width an color_mc.height.
01:16On the next line of code,
01:18I'm going to connect our BitmapData object to our color movie clip on Stage
01:23by using the BitmapData's draw method.
01:27Once you use the draw method on a display object, you can access the pixels of that display object.
01:33In the showColor function I'll set the value for the color variable
01:36equal to pixelData.getPixel.
01:40This grabs a pixel at a certain X and Y position and the X and Y position I'll use is the mouse X and Y position.
01:47So this.mouseX and this.mouseY.
01:51Then I have a trace statement tracing the value of color. So let's test the movie and see what we get.
01:55You may be surprised to see that the color is not a hexadecimal representation of the color.
02:01Rather the values range from zero, for all the way black,
02:05to 16,777,215 for all white. Hexadecimal values are actually a shorthand way of working with colors.
02:15In a hexadecimal color value, each two digits represent red, green and blue respectively,
02:21and they both have values of zero through nine and then A through F.
02:25This allows you to give a value between zero and 255, or 256 total values of color for each channel.
02:33Red, green or blue.
02:35The 16 million plus number comes from multiplying 256 by 256 by 256.
02:43So using a colored number isn't quite useful to us.
02:47One thing we can do is get the color information as a hexadecimal value. If you trace color.toString and then pass in 16,
02:5716 means you want a 16 digit base for your string representation of the number.
03:03If you don't pass any value for toString, then the default value is the 10 digit base, which is used for most numbers.
03:09Let's test the movie and see what we get here.
03:12So I'm getting my hexadecimal red value, green,
03:15and then blue.
03:17Notice the hexadecimal zeroes on the left side are cut out though the zeros on the right side are always included.
03:24I'll close the preview window now as well as the Output window.
03:27Now we could isolate the colors by manipulating the string,
03:31but there's actually a more convenient way.
03:33But what we have to do to is break the color down into bits
03:38and access the data on a bit level.
03:41I'm going to scroll down in the code a little and I'm going to talk about some code that I have commented in regards to bits.
03:46You may have heard the term 24-bit color before.
03:49In 24-bit color each channel, red, green and blue, gets 8 bits.
03:55So 8 bits for red and 8 bits for green and then 8 bits for blue.
03:59Bits can have a value of one or zero, so full whites would be all one's.
04:04Full red, is all the red bits with one and all the rest of the bits have zero.
04:09Full green.
04:10The green bits have one,
04:12the blue bits have zero. Notice that I didn't write the red bits here,
04:17because it's the number isn't high enough, then there's no need to add in extra zero's
04:22on the left side.
04:24And then we have full blue,
04:25which doesn't have any values to the left of it.
04:28So if you want to see this in action, you can scroll up
04:31and instead of getting a hexadecimal 16 string, we pass in a value of two into toString. Then we get a two digit number
04:40string, which means we either get one's or zero's.
04:43So this will convert our color string into binary or bit data.
04:48Test the movie.
04:50Roll over the colors and just like I wrote below, blue gets all one's,
04:54green has ones for the green channel,
04:56zero's for the blue channel.
04:59Red has ones for the red channel, zeros for the other channels.
05:02Black is simply just zero
05:04and white it is all one's.
05:06Now what we have to do
05:08in order to access the data is we have to shift the bit information over.
05:14So if we want just the red data, we have to shift those red bits 16 places to the right.
05:21So what I'm going to do is I'm going to write that in code here.
05:24So the value of red is going to be color, and the way that we shift bits
05:28is by using two greater than signs,
05:31ActionScript for bitwise shift right.
05:35We shift it to the right 16 spaces,
05:38and now what we'll do it uncomment this line of code
05:41that traces the red value. I'll comment out the other one that traces of color value in binary form,
05:46just so it's not confusing in the Output window,
05:49and test the movie.
05:50And rollover red. So we get red is 255 when we rollover that. Then we have red is zero when we roll over the colors that are not red.
05:59So that was nice and easy.
06:02Unfortunately it's not quite as easy to get the values for green and blue, but it's not too bad.
06:07For green, we'll take color, we'll shift all the bits.
06:12When we shift the bits eight spaces to the left, we still have the information for the red channel,
06:17which we don't want. So what we need to do is have a way to get rid of that. The way we'll do that is using the bitwise AND operator.
06:25The bitwise AND operators is one ampersand (&) and what it does is it compares bits.
06:31Now let's type the code and we'll talk about it in just a second. Type in bitwise AND operator and after that type in OxFF.
06:38That's the hexadecimal for 255. Let's scroll down back into our code and take a look at the bit information.
06:45Let's picture full green
06:47being compared with full red.
06:50Let's say we have full red and we wanted to get the green for that color.
06:56So we want to find at how much green is in our red color. So we do the bitwise shift.
07:01So we shift it eight pixels to the right and we're left with this information I have highlighted,
07:07the red information and the green information.
07:10What the bitwise operator does is it compares that information with
07:15255, which in binary is eight one's.
07:20So eight one's gets compared with
07:28eight one's
07:30and eight zero's.
07:33So this is our red color here, once it's shifted,
07:36and that gets compared with 255. Notice there is no information to the left.
07:43The bitwise AND operator compares bits.
07:47And if the bits are both one,
07:49then the bitwise AND operator takes that as a one. And if the bits are not both one,
07:54then the bitwise AND operator converts that data to zero.
07:58So basically what happens here in comparing these two bits of data,
08:03the one's that were originally for our red channel get thrown out because they are compared against nothing.
08:09And if the one's that we want for the green channel,
08:12are one's then they remain ones and if they're zero,
08:15then they remains zero's.
08:17So there's a look at how it works.
08:20Let's scroll up in the code
08:22and uncomment the trace green line of code. Test the movie and find accurate green values when you roll over the colors.
08:32Last thing you do is set the value for blue. The value's going be color,
08:37and we don't need to shift it because the blue data is all the way to the right, but we do have the data for the other
08:44colors that's to the left of that. So we need to use our bitwise AND operator
08:49and compare that against 255, or 0xFF, to get rid of all the bit data to the left.
08:56Let's uncomment the trace blue line of code
08:59and test the movie
09:02and you'll find accurate information for each color.
09:07So now that we've done that,
09:08we can access the pixel information for any object and we can use those in our particle system.
Collapse this transcript
Triggering particles based on pixel colors
00:00Now we'll take a look at making particles based on the pixels of an image. If you're following along, I'm working in Pixels.FLA
00:06in the movie two folder.
00:08On the Stage I have a simple movie clip that's just a fish with a white mouth.
00:12We want the bubble particles to come out of the fish's mouth.
00:15If you test the movie,
00:18you'll see that they sporadically come out of the mouth of the fish.
00:21Let's take a look how this works.
00:23Go to the first keyframe of the actions layer and open up the actions panel.
00:27In the top of our code, we have our pixelData object.
00:31It's the width and height of the fish, and I passed in two other optional parameters: whether the BitmapData object is transparent,
00:38I passed in false, and then I passed in a background color of black.
00:42And that's really significant because I'm looking for white pixels, so I definitely don't want this background color to be white.
00:49I'm going to scroll to the bottom of the code.
00:51I'm putting the container on the Stage and running the draw method of the BitmapData class,
00:56passing in fish_mc then I have my makeParticles function making particles.
01:02If we scroll up to the makeParticles function, we're creating two random values here, a random X and a random Y position.
01:09That's a random number based on the fish's width and height. Notice I data typed them to positive integers.
01:15This is a sort of a shortcut to data typing them as numbers and rounding the values.
01:20Data typing a floating point number to uint simply truncates that number and cuts off anything after the decimal point.
01:26In the conditional statement, the condition to create the particle is that we run the checkPixels function, passing in the
01:32random X and Y positions. If the value returned is true, we create a particle; if not, we don't. If you scroll down
01:38to the checkPixels function, in the color variable,
01:42we capture the pixel's color in the color variable, we isolate the red, green and blue values.
01:47And then I have a conditional statement set up to see if red is greater than ee,
01:52and green is greater than ee and blue is greater than ee.
01:56I could obviously modify this and just check to see if the color is lighter than a light gray
02:01and return true, but this way if I ever want use a different color I simply to change these color values.
02:07So that's all there is to it.
02:09I'll test the movie again and you see the bubbles coming out of the fish.
02:15So what's happening is random positions on the fish are being picked.
02:19All over the fish.
02:21If that pixel is white for that frame, then a particle is created.
02:24If not, then there's no particle created.
02:27Now what we can do is narrow down this looking. Because we're looking across the whole fish right now.
02:33What if we just wanted to take a rectangular area for the fish's mouth and just search in that area? There is actually a
02:38built-in method of the BitmapData class that'll help us do that. I'm going to create a variable at the top of the code here.
02:44And I'll call this mouthRect, with a capital R. This is going to represent a rectangle that's the bounding box for the fish's mouth.
02:53So I'm going to data type it to a rectangle,
02:56I'm going to scroll down.
02:57And then right under pixelData.draw,
03:00at the very bottom of my code, I'm going to type mouthRect and we'll set the value equal to pixelData.
03:09And then we'll run the getColorBoundsRect. The getColorBoundsRect method allows us to pass in a color value
03:17then Flash will look for the colors that we pass in,
03:21and create a color bounds rectangle around those colors,
03:25and that will allow us to then search within that rectangle instead of searching for the whole fish
03:30to find white pixels.
03:32So I'm going to pass in
03:340x and then eight F's.
03:37We're passing in eight F's because this is 32-bit color. 32-bit color simply includes alpha as the first two digits.
03:45This first parameter tells Flash which pixels to consider when looking for colors.
03:51So I'm simply passing in white here,
03:54with full alpha, that's why I passed in F's at the beginning and them the color is the color that we're searching for in
03:59the next parameter. So I'll type in 0x and then eight F's again.
04:04Then I'll close out the parentheses
04:06and I'll close out the method. Now let's scroll up in the code
04:10and for our random number,
04:12instead of getting a random number for the width of the fish,
04:15we'll get a random number based on the width and position of our rectangle.
04:25So I'll multiply Math.random
04:27by the width of our rectangle
04:29and then tack on the rectangle's X position.
04:35And we'll do the same thing for height.
04:45And now when we test the movie
04:48we're only having Flash search for white pixels
04:51in the rectangle surrounding the fish's mouth.
Collapse this transcript
In practice pt. 5: Adding particles to video
00:00Using the techniques in last few movies, we can add particle effects to video.
00:05All we have do is simply redraw the bitmap.
00:08Using the techniques in the last two movies, we can add a particle system to video.
00:12Let's take a look at this video on the screen here.
00:14(Music plays along with video.)
00:21Notice the video has a very prominent white area. What we're going to do is add particles wherever there's white in the video.
00:28If you're following along, open up Video.FLA
00:31in the Chapter 8, movie three folder. Let's go to the first keyframe of the actions layer and open up the actions panel.
00:38You'll see that this code is pretty much similar to the code in the last movie.
00:41We've got our BitmapData object.
00:44We have a rectangle to represent a white area in the video.
00:49And we're using the Draw method to put the pixels of the video
00:53inside of our BitmapData object. The only thing that we're missing here
00:58is running the draw method every frame.
01:01What I'm going to do is copy the code that runs the draw method, scroll up in my code and at the very top of the makeParticles function,
01:07I'm going to paste the code that I just copied.
01:10So every frame, we write the pixel data for the video into our BitmapData object.
01:15Then we draw the rectangle based on that data.
01:19From there, we do the same thing to check for the white values of pixels.
01:23If the pixel that's picked is white, then we create a particle. Let's test the movie and check it out.
01:29(Music plays along with video.)
01:38So you can use a particle system with any application, even with video.
Collapse this transcript
Conclusion
Goodbye
00:00Well that's it. I hope you had fun because I sure did.
00:04We talked about all kinds of information about particles, how to create a particle class and how to build a particle system,
00:09how to create different types of particles like rain and smoke
00:13and even add particles to video.
00:15I hope you found the particles aren't just cool looking, little animated things, but they're actually useful in real-world applications.
00:22So have fun using particles and I'll see you next time.
Collapse this transcript


Suggested courses to watch next:

ActionScript 3.0: Working with XML (2h 27m)
Todd Perkins



Are you sure you want to delete this bookmark?

cancel

Bookmark this Tutorial

Name

Description

{0} characters left

Tags

Separate tags with a space. Use quotes around multi-word tags. Suggested Tags:
loading
cancel

bookmark this course

{0} characters left Separate tags with a space. Use quotes around multi-word tags. Suggested Tags:
loading

Error:

go to playlists »

Create new playlist

name:
description:
save cancel

You must be a lynda.com member to watch this video.

Every course in the lynda.com library contains free videos that let you assess the quality of our tutorials before you subscribe—just click on the blue links to watch them. Become a member to access all 98,695 instructional videos.

start free trial learn more

If you are already an active lynda.com member, please log in to access the lynda.com library.

Get access to all lynda.com videos

You are currently signed into your admin account, which doesn't let you view lynda.com videos. For full access to the lynda.com library, log in through iplogin.lynda.com, or sign in through your organization's portal. You may also request a user account by calling 1 1 (888) 335-9632 or emailing us at cs@lynda.com.

Get access to all lynda.com videos

You are currently signed into your admin account, which doesn't let you view lynda.com videos. For full access to the lynda.com library, log in through iplogin.lynda.com, or sign in through your organization's portal. You may also request a user account by calling 1 1 (888) 335-9632 or emailing us at cs@lynda.com.

Access to lynda.com videos

Your organization has a limited access membership to the lynda.com library that allows access to only a specific, limited selection of courses.

You don't have access to this video.

You're logged in as an account administrator, but your membership is not active.

Contact a Training Solutions Advisor at 1 (888) 335-9632.

How to access this video.

If this course is one of your five classes, then your class currently isn't in session.

If you want to watch this video and it is not part of your class, upgrade your membership for unlimited access to the full library of 1,899 courses anytime, anywhere.

learn more upgrade

You can always watch the free content included in every course.

Questions? Call Customer Service at 1 1 (888) 335-9632 or email cs@lynda.com.

You don't have access to this video.

You're logged in as an account administrator, but your membership is no longer active. You can still access reports and account information.

To reactivate your account, contact a Training Solutions Advisor at 1 1 (888) 335-9632.

Need help accessing this video?

You can't access this video from your master administrator account.

Call Customer Service at 1 1 (888) 335-9632 or email cs@lynda.com for help accessing this video.


site feedback

Thanks for signing up.

We’ll send you a confirmation email shortly.


By signing up, you’ll receive about four emails per month, including

We’ll only use your email address to send you these mailings.

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

By signing up, you’ll receive about four emails per month, including

We’ll only use your email address to send you these mailings.

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked