Flash Animation If and Else Conditional

Flash Animation If and Else Conditional
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.

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.