|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.
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.
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.
Here is the code so far.
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.
We will place these instructions between the curly brackets that follow the else keyword.
Here is the complete code for the snowfall function.
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.