Variables and Flow Control
Top  Previous  Next


Description
This step-by-step tutorial develops the previous tutorial by introducing variable calculations and flow control. After the Movie displays the words "Hello World" in the Debug Panel, it will calculate the results of y = x * x in the Debug Panel, where x has the values of 0 to 10.
When the result y is greater than 50, it will add the additional comment "wow that is big".

Aim
This Movie introduces the onFrame() Event, demonstrates the use of the trace command and Debug Panel and demonstrates the use of variables, as well as the if and while conditional statements.

.swi file
"flowcontrol.swi"

1.   Load the .swi file "helloworld.swi". Ensure that the 'Play' button is not pressed and save the file as "myflowcontrol.swi".

2.   Enter an onFrame Event to Scene_1. This Event will occur when the Movie reaches the specified Frame.
scripttute2_1
As Frame 1 is the currently selected Frame in the Timeline, the onFrame Event will be for Frame 1 of Scene_1. After inserting the Event, the 'Script' Panel should look like this:  
scripttute2_2
Note:
·The Timeline now indicates via an 's' that a Scripting Event will occur in Frame 1 of Scene 1. If needed, this onFrame Event can be moved to a different Frame by altering the number in the bottom half of the 'Scripting' Panel. The Event can also be moved to a different Frame by dragging the 's' in the Timeline  
·The onLoad() event was entered in the previous tutorial (Hello World).  

2.Add the following statements to the onFrame (1) Event. Note that script can also be added by selecting the area where you want the script to be placed and then pressing the right mouse button to access the Add Script option. The Menu commands used to enter the script are shown under the Comments heading  

Statement
Comments.
x = 0;
Enter using Add Script | Statements | name = expr;
Type "x" into the Name field (this field defines a property or variable). Then add the value "0" (zero - with no quotes) into the value field below the Operator. In this case the Target: option can be left blank. This option is used to define the name of the Object that contains the property or variable. Leaving this value blank is the same as entering the value this
y = 0;
Enter using Add Script | Statements | name = expr;
Another way to enter this statement is to copy and paste the x=0 statement, then change to parameters to make this statement y = 0;
while (x <= 10) {
Enter using Add Script | Conditional | while (...) {
Enter x <= 10 into the lower dialog box
y = x * x;
Enter using Add Script | Statements | name = expr; or copy and modify previous statement
trace("y = " add y add " x = " add x);
Enter using Add Script | Debugging | trace(...)
Note that the add command is used to join multiple strings
if (y > 50) {
Enter using Add Script | Conditional | if (...) {
trace("wow that is big");
Enter using Add Script | Debugging | trace(...)
This command is only executed if the value of y is greater than 50
}
Entered as part of the if statement. Shows the end of the if statement
x = x + 1;
Click on the ending bracket for the IF statement, and enter this script using Add Script | Statements | name = expr;
If this statement is missing, x will never increase and loop will continue indefinitely.
Note that x++ or x += 1 statements could have also been used
}
Entered as part of the while statement. Shows the end of the while loop


Once you have entered the above script commands, your 'Script' Panel should look like this:  
 
scripttute2_3

3.To check your script, select the Debug tab among the Panels displayed on the right hand side and press the 'Play' button to run your Movie. The 'Debug' Panel should show the following:  

scripttute2_4
Analysis
When the 'Play' button is pressed, the Movie loads. This causes the script in the onLoad() Event Handling function to be executed.
The trace statement containing the string "Hello World" is then executed causing the string to be displayed in the Debug Panel.

Once the Movie has been loaded it starts playing. On frame 1 of Scene_1 the onFrame(1) Event Handling function containing the following script is executed:

x = 0
;
y = 0
;
while (x <= 10
{
   y = x * x;
   trace("y = " add y add " x = " add x);
   if (y > 50) {
      trace("wow that is big");
   }

   x = x + 1
;
}


x and y are both Variables. All variables should be defined a value before use. For this reason, the statement x=0 should be included as the subsequent statement x = x + 1 assumes that x already has a value. As the variable y is only assigned values, the y=0 statement is not strictly necessary, but it is always good practice to set up initial values.

The while loop will continue to execute the contained statements until the value of x exceeds 10. As the last statement of the while loop is x = x + 1, the loop will execute with the values of x = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 and terminate when x = 11.

The trace statement trace("y = " add y add " x = " add x); is used to display the values for both y and x. The add operator is used to join multiple strings into a single string. Note that the variables x and y are converted to strings for display in the Debug Panel.

On each iteration of the while loop, the value of y is compared to 50 by the if (y > 50) statement. If the variable y is greater than 50 then the statement contained by the if statement, trace("wow that is big"); is executed. This causes the line "wow that is big" statement to be displayed each time y is bigger than 50.