Tutorial: Random Die or Dice Rolls in Flash

Random Number Tied to Visibility in Flash

Neil Mey
What you will need

Graphics: A background image for your dice to roll on

6 die with numbers from 1 -6

step 1: in your flash action-script 2.0 file you created animate a die rolling onto the screen. You choose how many frames it will take to do this. I achieved the look I wanted in 12 frames. remember to animate a blur to give it the effect of speed and not to show to clearly any numbers on it.

Step 2: create 2 new layers. above your animation layer. Name them visibility and the other one random number

Step 3: move to one frame passed your animation, in my case it is frame 13 of my random number layer. Insert a blank keyframe and put in the following action-script on that keyframe.

numb1 =random(6)+1;

We are putting in the plus one because we want to get rid of zero as there is no die for zero.

Step 4: Create a new layer and name it dynamic text. Move to in my case frame 13 or in your case one frame passed your animation. On the stage create a dynamic text box and give it an instance name of numb1. Our random number script that we created in step 3 will place a number in that text box

Step 5: In your layer stack move dynamic text layer to the very bottom so that it is under your background layer. We do not want to see the number when I die rolls

Step 6: Create a new layer called die roll. From your library place all the die images on top of each other on the stage. Don't worry we will hide these images later with action-script and only the one we want will show. For each die give them an instance name according to the number it represents.

dice_1

dice_2

dice_3

dice_4

dice_5

dice_6

Step 7 Move to your layer you named "visibility". On frame 13 of this layer or one frame passed you animation of the die flying in, insert a blank keyframe and put the following action-script on it. This action-script will hide the images on the stage.

_root.numb1 =random(6)+1;

_root.dice_1._visible = false

_root.dice_2._visible = false

_root.dice_3._visible = false

_root.dice_4._visible = false

_root.dice_5._visible = false

_root.dice_6._visible = false

Step 8: With your action-script window still open add the following script to what you just typed.

if (_root.numb1 == 1) {

_root.dice_1._visible = true

} else if (_root.numb1 == 2) {

_root.dice_2._visible = true

} else if (_root.numb1 == 3) {

_root.dice_3._visible = true

} else if (_root.numb1 == 4) {

_root.dice_4._visible = true

} else if (_root.numb1 == 5) {

_root.dice_5._visible = true

} else if (_root.numb1 == 6) {

_root.dice_6._visible = true

}

Step 8: create a new layer and call it roll.

Step 9: create a new button symbol and name it roll. Design it however you like

Step 10: On your roll layer place your roll button symbol on the stage

Step 11: Right click on your button on the stage and put the following action on it.

on (release) {

gotoAndPlay("Scene 1", 1);

}

Step 11B: Do this step only if you want to skip the intro animation and have a new random die number appear. You would also do this if you plan on having more than one die on the stage that rolls and you want only certain ones to be re rolled.

Create another roll button or put another copy of your original roll button on the screen.

Right click on your button on the stage and put the following action-script on it

on(release) {

numb1 =random(6)+1;

_root.dice_1._visible = false

_root.dice_2._visible = false

_root.dice_3._visible = false

_root.dice_4._visible = false

_root.dice_5._visible = false

_root.dice_6._visible = false

if (_root.numb1 == 1) {

_root.dice_1._visible = true

} else if (_root.numb1 == 2) {

_root.dice_2._visible = true

} else if (_root.numb1 == 3) {

_root.dice_3._visible = true

} else if (_root.numb1 == 4) {

_root.dice_4._visible = true

} else if (_root.numb1 == 5) {

_root.dice_5._visible = true

} else if (_root.numb1 == 6) {

_root.dice_6._visible = true

}

}

Step 12: If you have not saved your file save it now. File publish preview to test out your movie

Published by Neil Mey

My name is Neil Mey. I am from Saint Louis Missouri and have a Master of Arts degree in Communications from Lindenwood University. I am currently an Instructor at Lindenwood University as well.  View profile

To comment, please sign in to your Yahoo! account, or sign up for a new account.