Flash Tutorial - [ Intemediate ]
Action Scripting - Variables
This tutorial is going to teach you the basic principles of Variables
and their use in Flash Action Scripting.
What is a Variable anyway?
Everyone will stuck about this term now and then. It is every where in the computer. You can remember me by my name ("Madhukar") and it is obvious due to, we have brain to remember. But, computer don't have such a facility to remember. So, an identifier is required to keep such values intact; so, is needed this variable. One more concern is that, it's value frequently changes during the process.
In Flash, variable declaration have simplest form:
Variable_name = some value;
for instance,
message="hello! how are you?"; (A)
number_value=10; (B)
Both (A) & (B) are the examples of variables. Both holds certain type of values. Variable (A) type of declaration is a string type, which holds string value. String values are usually enclosed with in a double quote. Variable (B) type of declaration is a numeric type. Remember, both are variables of constant values. This is the reason why, these type of variables are called as Constant Variables.
Let us assume another example, where the values of variable frequently changes,
SUM_OF_A_AND_B = NUMBER_ONE + NUMBER_TWO; (C)
A variable becomes a pure variable (varies), when the value of the variable changes during the execution of a program, not at the time of declaration.
Let us see a small example to explore it well.
1. Go to File > New (To open a new movie file, default name with "Movie1"). It's dimension is leaving up to you.
2. Click on Oval Tool and draw two oval symbols or any (your choice, either rectangle or oval; in this case i preferred to choose oval) on the stage. This is because to convert those two oval symbols into a button. So, now click on the Text Tool, draw on the first oval symbol and type some text, say "Greeting". Now, select both text box and first oval symbol and go to Modify > Group to make as single entity. Similarly, click on the Text Tool and type some text, say "Say, Reply" and drag on to the second oval symbol. Now, select both text box and second oval symbol and go to Modify > Group to make as another single entity.
3. Select the first oval symbol, go to Insert > Convert to symbol. Choose "Button" as type of "behaviour". Again do the same for the next button. Choose the second symbol, go to Insert > Convert to symbol. Choose "Button" as type of "behaviour".
4. Now, again select Text Tool and draw on the stage. In it's "Text Options"(Text > Options), choose "Dynamic Text" from drop down menu. Under "Variable" text, in the text box, specify it's identification, i.e. now we are declaring a variable with name say, "Message". Leave other options as it is, no matter what ever options you made are not concerned here in this example.
5. Now, an important step has to be taken. I think you probably come across many a programming language such as Visual Basic, C++ and others. In OOP's, it is common that, objects are present everywhere and it has properties, behaviour, methods & events. I think you got my point.
6. Now, right click on the first button, choose Actions, "Object Actions" dialog box will be displayed. Double click on "On Mouse Event" from the Object Actions dialog box. It displays several events of the "button" object. Check "Press" event and uncheck "Release" event. Now, select "set variable". It takes two arguments, Variable and Value. In the first text box, type the variable name "Message" (already we specified see, step 4 in this section). Under "Value", type "Hi! How Are You?". Now, close the window "Object Actions".
7. Again for the second button on the stage. Right click on the second button, choose Actions, "Object Actions" dialog box will be displayed. Double click on "On Mouse Event" from the Object Actions dialog box. It displays several events of the "button" object. Check "Press" event and uncheck "Release" event. Now, select "set variable". It takes two arguments, Variable and Value. In the first text box, type the variable name "Message" (already we specified see, step 4 in this section). Under "Value", type "Fine, Thank You Friend!". Now, close the window "Object Actions".
8. Now, press Ctrl + Enter Key strokes to view the result. This is how the variable acts and holds the value.
Happy Programming, have a fun!!!
"Fundamentals are the Route
to the Destination" - BHMK
Go Home