Event Listeners, Functions and Flash Particles

Now that it is January, it is time to make it snow in our FlashR CS3 particle system. We will finally add more snowflakes to our snow scene. After all, we want to see how it looks so that we can make any final adjustments before we can consider this project finished.

So, how do we create several snowflakes just like the one we have been working on for the last few tutorials? There are several ways to do this but this is a beginner's tutorial. Let's do it the easy way. So far we have accomplished two tasks. First we created a snowflake and then we programmed Flash to run the snowfall function to move the snowflake down the stage. The reason the snowfall function can move the snowflake down the stage is because we have told Flash to run the function for every new frame. With each new frame the snowflake moves down the stage a little more.

So it makes since that we could change this code to create a new snowflake with every new frame. Let's give it a try. Here is the code that creates one snowflake.

In order to use the event listener (line one of the code) to create a new snowflake on every frame, we need to convert the above code into a function that will be the event handler for this event listener. That's easy enough. Let's call this new function "snowflake".

function snowflake(event:Event):void{ }

Now we can place all of the above code inside the curly brackets to create our new function.

Let's test again. Ok, no error messages this time. Also, we are getting all the snowflakes that we need but they are not falling. Why? Our problem is in line one of the code. When we changed the name of the event handler from "snowfall" to "snowflake" in the event listener, we ceased to run the snowfall function. Now it is just sitting there, but we never tell Flash to run the function. We need to find a way to call this function and our snowflakes will fall again.

In the last tutorial, we used the ENTER_FRAME event listener to programmed Flash to run the snowfall function with every new frame. And we know that we can't call the snowfall function until we create a snowflake for it to animate. So, it is only logical that we will placed the snowfall function call after the section of code that creates the snowflake. Let's add a second ENTER_FRAME event listener that will call the snowfall function. We will place this code after the code that creates our snowflake.

Test your movie again. It works. But our flakes are ignoring the left side of the stage. Let's work on that next. For now, save your snow scene as SnowScene8.fla.

Note - This animation code is not complete until you reach the end of the tutorial series. Do not use this incomplete code in your Flash projects.