logo
g Text Version
Beauty & Self
Books & Music
Career
Computers
Education
Family
Food & Wine
Health & Fitness
Hobbies & Crafts
Home & Garden
Money
News & Politics
Relationships
Religion & Spirituality
Sports
Travel & Culture
TV & Movies

dailyclick
Bored? Games!
Nutrition
Postcards
Take a Quiz
Rate My Photo

new
Action Movies
Bible Basics
Houseplants
Romance Movies
Creativity
Family Travel
Southwest USA


dailyclick
All times in EST

Clairvoyance: 08:00 PM

Full Schedule
g
g Flash and Animation Site

BellaOnline's Flash and Animation Editor

g

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.





Add Event+Listeners%2C+Functions+and+Flash+Particles to Twitter Add Event+Listeners%2C+Functions+and+Flash+Particles to Facebook Add Event+Listeners%2C+Functions+and+Flash+Particles to MySpace Add Event+Listeners%2C+Functions+and+Flash+Particles to Del.icio.us Digg Event+Listeners%2C+Functions+and+Flash+Particles Add Event+Listeners%2C+Functions+and+Flash+Particles to Yahoo My Web Add Event+Listeners%2C+Functions+and+Flash+Particles to Google Bookmarks Add Event+Listeners%2C+Functions+and+Flash+Particles to Stumbleupon Add Event+Listeners%2C+Functions+and+Flash+Particles to Reddit



 



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


For FREE email updates, subscribe to the Flash and Animation Newsletter


Past Issues


print
Printer Friendly
bookmark
Bookmark
tell friend
Tell a Friend
forum
Forum
email
Email Editor


Content copyright © 2014 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.

g


g features
Particle System - Emitter Shape Parameters

Motion Particle Systems - Introduction

Particle Fog in Motion 5 - Scene Graphics

Archives | Site Map

forum
Forum
email
Contact

Past Issues
memberscenter


vote
Poetry
Daily
Weekly
Monthly
Less than Monthly



BellaOnline on Facebook
g


| About BellaOnline | Privacy Policy | Advertising | Become an Editor |
Website copyright © 2014 Minerva WebWorks LLC. All rights reserved.


BellaOnline Editor