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
European Travel
Action Movies
Bible Basics
Houseplants
Romance Movies
Creativity
Family Travel


dailyclick
All times in EST

Low Carb: 8:00 PM

Full Schedule
g
g Flash and Animation Site

BellaOnline's Flash and Animation Editor

g

Controlling Flash Animation with If and Else Conditional Statements


Now that we have programmed our FlashR CS3 particle system to make our snowflake movie clip fall down the stage for our snow scene animation, we are almost done with this snowflake. However, as the movie clip falls down the entire stage, what happens when it falls below the stage? As we have it programmed now, the flake will just keep falling.

It would be better if we could stop the animation after the flake gets beyond the display area of the stage. After all, why should we make Flash work harder then necessary? So, what can we do? We can stop the animation. We will do this by adding a conditional statement to our existing code.

How to Stop the Animation

As our code stands now, we used the ENTER_FRAME event listener to start, or call, the event handler function that creates the animation. So it would be understandable that we can stop the animation by removing the event listener. Without the event listener, the event handler function will not be called and the snowflake movie clip will stop falling. This code will remove our event listener.

stage.removeEventListener(Event.ENTER_FRAME, snowfall);

But we still need to tell flash when to remove to event listener. We need to add a conditional statement to the snowfall function code. This conditional statement will tell Flash under what conditions it should stop the animation. Because we want the snowflake to stop after it disappears from the stage, we could tell Flash to remove the event listener when the flake's Y position is greater than 400, which is the height of our stage.

It is easy to code a conditional statement. You start the statement with the if keyword and then add the condition that you want Flash to test. This condition will go between parentheses. For example, we could tell Flash to test if the Y position of _Snowflake is greater than 400.

if ( _Snowflake.y> 400 )

For our conditional statement, this condition will be either true or false. Next, we need to tell Flash what to do if the condition is true (the Y position is greater than 400). As we have already decided, we will remove the event listener. We will put these instructions between curly brackets.

{ stage.removeEventListener(Event.ENTER_FRAME, snowfall); }

Here is the code so far.

if ( _Snowflake.y> 400 )
{ stage.removeEventListener(Event.ENTER_FRAME, snowfall); }

But we want the snowfall animation to continue if the condition is false and the Y position is not greater than 400. We can do this with the else statement. As you probably have figured out, the else statement begins with the else keyword. After the else keyword, we need to tell Flash what we want to do. We want Flash to add 10 pixels to the Y position of our movie clip. We have already written this code in the last tutorial.

_Snowflake.y += 10;

We will place these instructions between the curly brackets that follow the else keyword.

else
{ _Snowflake.y += 10; }

Here is the complete code for the snowfall function.

function snowfall(event:Event):void
{
if ( _Snowflake.y> 370 )
{ stage.removeEventListener(Event.ENTER_FRAME, snowfall); }
else
{ _Snowflake.y += 10; }
}

Test your movie and the snowflake will stop after it leaves the stage. What? If you can't see it, then you don't believe it. OK, let's try another effect. We will make the flake stop at the bottom of the stage. Change the 400 in the if statement to 370 and test again. When you test the movie again, the flake will stop at the bottom of the stage. Save your snow scene as SnowScene5.fla.





Add Controlling+Flash+Animation+with+If+and+Else+Conditional+Statements to Twitter Add Controlling+Flash+Animation+with+If+and+Else+Conditional+Statements to Facebook Add Controlling+Flash+Animation+with+If+and+Else+Conditional+Statements to MySpace Add Controlling+Flash+Animation+with+If+and+Else+Conditional+Statements to Del.icio.us Digg Controlling+Flash+Animation+with+If+and+Else+Conditional+Statements Add Controlling+Flash+Animation+with+If+and+Else+Conditional+Statements to Yahoo My Web Add Controlling+Flash+Animation+with+If+and+Else+Conditional+Statements to Google Bookmarks Add Controlling+Flash+Animation+with+If+and+Else+Conditional+Statements to Stumbleupon Add Controlling+Flash+Animation+with+If+and+Else+Conditional+Statements 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
Motion Workspace - Checkbox, Dials and More

Motion Workspace - Menu, Icons and Sliders

The Motion Toolbar Tools

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