Flash CS3 and AS 3.0 - Snow Scene - 2

Flash CS3 and AS 3.0 - Snow Scene - 2
This line of code will create the function which we will call "snowfall". The void after the parentheses tells Flash that no data will be returned by this function. Next, we will talk about the information that we will put inside the parentheses.

function snowfall(snowflake:MovieClip, movement:Number):void

The first piece of information inside the parentheses tells Flash that our function will be applied to a MovieClip named "snowflake". But wait! Our movie clip instances are snowflake1_mc, snowflake2_mc and snowflake3_mc. Why did we use "snowflake" inside the parentheses? This "snowflake" is a variable that can hold the name of any of the three instances. By using a variable instead of the actual name of the instance, we can use the same code to move any flake on the stage by passing a different instance name to this "snowflake" variable.

The second piece of information inside the parentheses tells Flash how we want to move the flake. Just as "snowflake" is a variable that will hold the instance name of any flake, "movement" is a variable that will hold the Number of pixels that we wish to move the flake. By using this "movement" variable, we have the option to move each individual flake a different number of pixels.

Let's skip down to the three lines of code that will "call" the function and pass the expected information to our variables.

snowfall(snowflake1_mc, 10);
snowfall(snowflake2_mc, 20);
snowfall(snowflake3_mc, 30);


As you can see, the snowfall function is called three times with the same basic function call.

snowfall();

The difference is in the information that is passed each time the function is called. The first function call tells Flash to move snowflake1_mc 10 pixels. The second call moves snowflake2_mc 20 pixels and the third call moves snowflake3_mc 30 pixels.

Now we only have to write the body of the function. This code is what will cause the flakes to move. The code for the body of a function is placed between curly brackets.

{
snowflake.y = movement;
}

Here we encounter our snowflake and movement variables again. The .y after the snowflake variable tells Flash to move the flake alone the Y axis. The equal sign assigns to the snowflake.y the number of pixels passed by the movement variable. I know this is a difficult concept to follow. If we could magically see inside the two variables, the code would look like this.

snowflake1_mc.y = 10;

The number 10 is passed from the movement variable and the instance name snowflake1_mc is passed from the snowflake variable.

When you test your movie, you will notice that the snowflakes have moved down from the top of the stage just as we wanted them to do. However, they are not moving. This is because we have only moved them once. To keep them falling down the stage, we need to change our code a little.

For reference, save your Flash movie as SnowScene1.fla. We will be using functions and function calls to run our particle system animation.

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.

Back





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.