Variables in Flash

This lesson will give you a brief introduction to variables in Flash and how they can be used.

 1. Run Flash 4.0 from the Start menu by choosing Macromedia Flash 4 and then Flash 4 to obtain a blank stage for the first frame. If you are already in Flash use the New icon from the Tool Bar to create a new project.

2. Click on Modify, then Movie from the pull-down menu to obtain the Movie Properties dialog box. Set the size of the movie to a Width of 300 and a Height of 200. Set the Background color to a Dark Blue. Click OK to continue.

3. Click the Text tool, set the Text Color to White and the Font to Arial, Bold, 20. Type Length Convertor. Place this text box at the top of the Stage as a title. Make sure to deselect this text box when done.

4. Click the Text tool again and change the font size to 18. Click the tool as shown here from the Modifier area to make this text box a text field (which will be used to get user input). Click the Paragraph modifier and choose Center.

5. Create two small Text Fields using this tool. Make sure the Text Color is black. Place them side by side. Click away from these two text fields to make sure that neither one is selected. Unclick this modifier and use the Text tool to finish setting up the Stage as shown here.

6. Right-click over the first Text field and set the variable name to in by typing into the variable box. Make sure that Disable editing is not checked and that Draw border and background is checked.

7. Right-click over the second Text field and set the variable name to cm by typing into the variable box. Check Disable editing for this field and make sure that Draw border and background is checked.

8. Add a Dial button from the Buttons Library. Make it smaller and place it at the bottom-center part of the Stage. Add a small Text box to the side and type : Calculate. Make sure the Text Color is White and that it is a text box, not a text field.

9. Right-click over the button, select Properties, then Actions.

10. Click on the Plus sign to add an Action and choose Set Variable.

11. Type cm in the Variable box.

12. Beside the Value box, click the abc button and choose Expression. Type 2.54*in in the Value box.

Note: There are 2.54 cm in each inch so the expression, 2.54*in takes the number that is typed in the box labeled in and multiplies it by 2.54 and the results are displayed in the box labeled cm. You can use a variety of different mathematical formulas here to do all sorts of calculations.

13. Now create a Reset button and set the Actions as shown here using similar procedures as above:

 

14. Your Stage should now look similar to this:

15. Click the Play button on the Controller to view this animation.

16. Save this file as lesson11.fla.

17. Use the File, Export command to obtain the Export Movie dialog box. Locate a directory where you are storing your files and give this movie a name, ie lessson11.

18. Accept the defaults from the Export Flash Player dialog box. Click OK to conclude the Export process.

The finished Movie should look similar to the one below.

You can use similar procedures to set up most any type of calculator. A more advanced use of variables could involve setting up something to keep score based upon a button that the user has clicked on. See the Flash Demos and the Sample Quiz to see this in use.