Event Listeners, Functions and Flash Particles

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.

var _Snowflake:mcSnowflake;

_Snowflake = new mcSnowflake();
addChild(_Snowflake);
_Snowflake.x = Math.random() * 510;
_Snowflake.y = Math.random() * 40;
_Snowflake.scaleX =_Snowflake.scaleY = Math.random();
_Snowflake.alpha = Math.random();


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.

function snowflake(event:Event):void
{
var _Snowflake:mcSnowflake;

_Snowflake = new mcSnowflake();
addChild(_Snowflake);
_Snowflake.x = Math.random() * 510;
_Snowflake.y = Math.random() * 40;
_Snowflake.scaleX =_Snowflake.scaleY = Math.random();
_Snowflake.alpha = Math.random();
}


Now, in line one of the code, we need to change the name of the function from "snowfall" to "snowflake". This will tell Flash to run the snowflake function with every frame.

stage.addEventListener(Event.ENTER_FRAME, snowflake);

Test your movie. What happened? We are getting error messages? Why?

Access of undefined property _Snowflake.

This error message tells us that we need to put the snowfall function code inside the snowflake function. So, let's drag that code inside the curly brackets.

function snowflake(event:Event):void
{
var _Snowflake:mcSnowflake;

_Snowflake = new mcSnowflake();
addChild(_Snowflake);
_Snowflake.x = Math.random() * 510;
_Snowflake.y = Math.random() * 40;
_Snowflake.scaleX =_Snowflake.scaleY = Math.random();
_Snowflake.alpha = Math.random();

function snowfall(event:Event):void
{
if ( _Snowflake.y> 370 || _Snowflake.x> 550)
{ stage.removeEventListener(Event.ENTER_FRAME, snowfall); }
else
{ _Snowflake.y += 5; _Snowflake.x += Math.random() * 10; }
}

}


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.

Copyright 2018 Adobe Systems Incorporated. All rights reserved. Adobe product screen shot(s) reprinted with permission from Adobe Systems Incorporated. Adobe, Photoshop, Photoshop Album, Photoshop Elements, Illustrator, InDesign, GoLive, Acrobat, Cue, Premiere Pro, Premiere Elements, Bridge, After Effects, InCopy, Dreamweaver, Flash, ActionScript, Fireworks, Contribute, Captivate, Flash Catalyst and Flash Paper is/are either [a] registered trademark[s] or a trademark[s] of Adobe Systems Incorporated in the United States and/or other countries.





RSS
Related Articles
Editor's Picks Articles
Top Ten Articles
Previous Features
Site Map








Content copyright © 2023 by Diane Cipollo. All rights reserved.
This content was written by Diane Cipollo. If you wish to use this content in any manner, you need written permission. Contact Diane Cipollo for details.