To use the "random"
function we first have to understand that it does. The expression
-
** variableX = random(NNN) **
where NNN is any number, will return an integer in VariableX from 0 to
NNN-1.
REVIEW
1. Open the Duplicate_Movie_Clip.fla that you created last time or
download it from ????????.
2. In scene 1 layer 2 frame 1 you should have an action script of
"var x = 1". In frame 2 you should have an action script of
"stop( )".
3. In scene 1 layer1 frame 1 your fly movie clip should have an
instance name of "fly_1"
4. The action script in the Add button looks like this.
NEW SCRIPTS
5. We are now ready to start modifying the script for multiple random instances!
With your select tool click on your Add button
to highlight it.
6. On the actions panel, change to expert mode.
- This action script will be triggered by the press of the mouse on
the Add button
- In this action script we will set up new variables to be used
elsewhere (numswatted, numfly).
- We will create a "Do while" loop to create a random number
instances per click from 1 to 10;
- We will randomly change the percent x and y scale property for the
fly, from 1 to 100.
- We will randomly change the position (x and y coordinates) that the
fly movie instance appears on the stage.
Your script should look like this:
7. Save and Play your movie. You will see that you will increase the number of
movie clips from 1 to 10 with each click of the button. With this example the
fly_ clips are loaded at different _x, _y coordinates, and the flies are
different sizes.
8. We are now ready to delete some of the flies.
9. Drag another button on to the stage lower left corner, and using you
text tool type "SWAT" on the button.
10. With your select tool click on the add button to highlight it.
11. On the actions panel, change to expert mode.
- This action script will be triggered by the "press" of the
mouse on the delete button
- We will create a "Do while" loop to delete a random number
instances per click from 1 to 10
- We will test using an if statement that there are still enough
flies
- We will test that there is always 1 fly left to prevent errors in
the index. (Indexes can not go negative)
Your script should look like this:
12. Play your movie. You will see that you will decrease the number of
movie clips from 1 to 10 with each click of the button.
13. Finally lets add a few displays to see how many
flies we have and how many we swat.
- Insert New Symbol, name it "Number Swatted" and make its
behavior movie.
- Take you rectangle tool, make the colour dark blue, and draw a
rectangle 150 pixels wide by 75 pixels high
- Select your text tool size 20 fill white and type "Number
Swatted"
- Select your text tool size 20 fill white and type "00" and
give it a variable name of "varnumswatted"
- Insert New Symbol, name it "Number of Flies" and make its
behavior movie.
- Take you rectangle tool, make the colour dark blue, and draw a
rectangle 150 pixels wide by 75 pixels high
- Select your text tool size 20 fill white and type "Number of
Flies"
- Select your text tool size 20 fill white and type "01" and
give it a variable name of "varnumfly"
14. Go to Scene 1 drag the symbol "Number Swatted" on to the
stage down at the bottom of the stage.
- Select the "Number Swatted"
- Open your Action Panel in expert mode.
- To access a variable we have to know what level it is on. Most
variables should be on _level0.
- This action will be trigger by a mouseup.
Your script should look like this:
15. Go to Scene 1 drag the symbol "Number of Flies" on to the
stage down at the bottom of the stage beside "Number Swatted"..
- Select the "Number of Flies"
- Open your Action Panel in expert mode.
- To access a variable we have to know what level it is on. Most
variables should be on _level0.
- This action will be trigger by a mouseup.
Your script should look like this:
16. Play your movie. You will see that you when you click ADD the
number in the "Number of Flies" increases randomly by 1 to 10,
when you click SWAT the "Number Swatted" if from 1 to 10, and
the number of flies decreases by the same amount.
|