• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Lesson 22. Optimizing Scripts > Using a Global Variable

Using a Global Variable

In the previous lesson, you experimented with some different speeds for the robot arm's up and down movements. First you moved the arm 1 pixel for each stillDown event, and then you sped it up by moving it 3 pixels for each stillDown event. When you made those changes, you had to edit each handler separately. In that case, the process wasn't so difficult because only two handlers were affected, but imagine a case in which you have several objects whose speed you want to change, or suppose you want to experiment with different speeds before settling on a final one. As you have probably guessed by now, you can streamline your work by creating a variable for this value. That's what you will do in this task.

1.
Double-click cast member 38 to open the movie script. At the top, add the lines shown here in bold:

							global gArmSpeed
							--This global variable will specify the number of
							--pixels the arm should move.
							on startMovie
							set gArmSpeed = 3
							--This will move the arm 3 pixels at a time.
							end
						

The startMovie message is a built-in message that Director sends just before the playback head enters the first frame of a movie. Therefore, a startMovie handler is a good place to include any commands that perform any preparatory operations needed before any action takes place in the score. For example, a startMovie handler might include Lingo commands that find out whether the movie is running on a Windows or Macintosh system and then, depending on the results, branch to a different frame of the movie. A startMovie handler is also a good place to initialize global variables, and that is what you have done in this case. In this script, you initialized the global variable gArmSpeed and set its initial value to 3. Recall from Lesson 20 that you can declare a global variable at the top of the script, and that it is customary to start a global variable name with the letter g. This convention is helpful because it enables you to determine whether a variable is global or local simply by looking at it.

2.
In the MoveArm handler later in the movie script, replace 3 with gArmSpeed and update the comments as shown here in bold:

on MoveArm direction
  repeat while the stillDown           --While the mouse button is held down,
    case (direction) of                --if the direction is
      1:                               --1: Move the arm up.
        currVert = sprite(1).locV – gArmSpeed
							--Set currVert to the vertical
							--location of (sprite 1) – gArmSpeed.
        if currVert > 0 then           --If the sprite is not at the top of the
           sprite(1).locV = currVert   --stage, then move the arm up gArmSpeed pixels.
        end if
      2:                               --2: Move the arm down.
        currVert = sprite(1).locV + gArmSpeed
							--Set currVert to the vertical
							--location of (sprite 1) + gArmSpeed.
        if currVert < 271 then         --If the sprite is not at the bottom of the
           sprite(1).locV = currVert
							--stage, then move the arm down gArmSpeed pixels.
        end if
      3:                               --3: Extend the arm.
        currCast = sprite(1).memberNum
                                       --Initialize currCast to the cast
                                       --member number of sprite 1.
        if currCast > 1 then           --If the arm is not already fully extended,
           currCast = currCast - 1     --subtract 1 from the variable
           sprite(1).memberNum = currCast
                                       --and set the cast member number
                                       --of sprite 1 to the new value of currCast.
        end if
      4:                               --4: Retract the arm.
        currCast = sprite(1).memberNum
                                       --Initialize currCast to the cast
                                       --member number of sprite 1.
        if currCast < 27 then          --If the arm is not already fully retracted,
           currCast = currCast + 1     --add 1 to the variable
           sprite(1).memberNum = currCast
                                       --and set the cast member number
                                       --of sprite 1 to the new value of currCast.
         end if
    end case
    updateStage
  end repeat
end


					  

3.
Close the Script window, save your work, and play the movie. Click the Up and Down buttons.

Everything works just as it did before. What's the advantage? You'll see in the next few steps when you change the speed of the arm again, this time to 10.

4.
Double-click cast memberm 38 to open the movie script. Make the changes shown here in bold:

global gArmSpeed             --This global variable will specify the number of
                             --pixels the arm should move.

on startMovie
  gArmSpeed = 10            --This will move the arm 10 pixels at a time.
end


					  

5.
Close the Script window, save your work, and play the movie. Click the Up and Down buttons.

Now the arm moves much faster, and you had to make a change in only one location.

6.
Double-click cast member 38 to open the movie script. Change the speed to 1 as shown here in bold:

global gArmSpeed             --This global variable will specify the number of
                             --pixels the arm should move.

on startMovie
  gArmSpeed = 1              --This will move the arm 1 pixel at a time.
end


					  

Now changing the speed is fast and easy.

You may be wondering why you created some variables locally and others globally. Look carefully at the movie script. You use three variables: currCast, currVert, and gArmSpeed.

You set the value of currCast to sprite(1).memberNum twice in the case statement, and you could have made it a global variable and initialized it in the startMovie handler.

For currVert, however, you set the variable to different values in the case statement, by either adding or subtracting the value of gArmSpeed. In this situation, it's better to keep the variable local because you need to assign it different values depending on the case.

Last, you created gArmSpeed. You could have made this a local variable and defined and initialized it in two different places in the case statement, and everything would still work. Here, however, the value of gArmSpeed will be constant throughout the movie; it will not change, regardless of the value of any other variable. Using a global variable to represent a constant value is a more sophisticated programming technique than creating a local variable whose value is reset in each handler that uses it.


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint