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

Lesson 22. Optimizing Scripts > Using a Local Variable

Using a Local Variable

You can use a local variable to represent a repeated value in the ExtendArm and RetractArm handlers. As you discovered earlier, using a variable also provides more flexibility. In Lesson 20, for instance, you learned that using a variable to refer to a sprite in a specific channel gave you the flexibility to change the sprite location without having to rewrite every script that referred to that sprite.

1.
Open the Start1.dir movie in the Lesson22 folder and save it as Robot2.dir in your Projects folder.

This is the finished version of the Robot.dir project from Lesson 21. If you completed Lesson 21, you can use your own Robot.dir movie instead. Just be sure to save it as Robot2.dir so that you still have your copy of Robot.dir.

2.
Double-click cast member 38 to open the movie script and look at the ExtendArm and RetractArm handlers.

Notice that sprite(1).memberNum is repeated throughout each handler. Each time you need to refer to this line, you must type it in its entirety—a process that is cumbersome and can easily lead to typing mistakes. You can make your life easier by using a local variable that represents the value sprite(1).memberNum. You will now change these handlers by creating a local variable, currCast, to represent sprite(1).memberNum throughout the handlers.

3.
Make the changes shown here in bold:

on ExtendArm
  repeat while the stillDown     --While the mouse button is held down,
    currCast = sprite(1).memberNum
							--Initialize currCast to the cast
							--member number of the sprite in channel 1.
							if currCast > 1 then
							--If the cast member in channel 1 is greater than 1 ,
							currCast = currCast -1   --subtract 1 from the variable to extend the arm
							sprite(1).memberNum = currCast
							--and set the cast member number of
							--the sprite in channel 1 to the new value of currCast.
    end if
    updateStage                  --Update the stage to show changes.
  end repeat
end
on RetractArm
  repeat while the stillDown     --While the mouse button is held down,
    currCast = sprite(1).memberNum
							--initialize currCast to the cast
							--member number of the sprite in channel 1.
							if currCast < 27 then        --If the cast member in channel 1 is less than 27,
							currCast = currCast + 1   --add 1 to the variable
							sprite(1).memberNum = currCast
							--and set the cast member number of the
							--sprite in channel 1 to the new value
							--of currCast to retract the arm.
    end if
    updateStage                  --Update the stage to show changes.
  end repeat
end


					  

These two handlers work exactly as they did before. In these lines you first create and initialize a new variable, currCast, giving it the value of sprite(1).memberNum. Then you replace most of the occurrences of sprite(1).memberNum with currCast. Notice that you cannot replace the occurrence of sprite(1).memberNum used to set the cast member for the sprite.

Because currCast is a local variable, you need to reinitialize it in every handler that uses it.

4.
Close the Script window, save your work, and play the movie.

Everything works just as it did before, but the new script is clearer and easier to maintain than the older version.

Next you will use a local variable to represent a repeated value in the MoveArmUp and MoveArmDown handlers.

5.
Double-click cast member 38 to open the movie script and look at the MoveArmUp and MoveArmDown handlers.

on MoveArmUp
  repeat while the stillDown         --While the mouse button is held down,
    if sprite(1).locV > 3 then       --if the vertical location of the
                                     --sprite - 3 is greater than the top of the stage,
       sprite(1).locV = sprite(1).locV - 3
                                     --move the arm up 3 pixels.
       updateStage                   --Update the stage to show changes.
    end if
  end repeat
end
on MoveArmDown
  repeat while the stillDown                --While the mouse button is held down,
    if sprite(1).locV < 268 then            --if the vertical location of the
                                            --sprite + 3 is less than 271,
       sprite(1).locV = sprite(1).locV + 3  --move the arm down 3 pixels.
       updateStage                          --Update the stage to show changes.
    end if
  end repeat
end


					  

Notice that sprite(1).locV is repeated throughout each handler. Each time you need to refer to it, you must type the entire line. Instead, you can use a local variable that represents the value of sprite(1).locV. You will create a local variable, currVert, to represent sprite(1).locV throughout the handlers.

6.
Make the changes shown here in bold and then close the window:

on MoveArmUp
  repeat while the stillDown                --While the mouse button is held down,
    currVert = sprite(1).locV
							--initialize currVert to the vertical
							--location of sprite1.
							if currVert > 3 then
							--If currVert -3 is greater than the stage top,
							sprite(1).locV = currVert -3
							--move the arm up 3 pixels.
       updateStage                          --Update the stage to show changes.
    end if
  end repeat
end

on MoveArmDown
  repeat while the stillDown                --While the mouse button is held down,
    currVert = sprite(1).locV
							--initialize currVert to the vertical
							--location of sprite 1
							if currVert < 268 then
							--If currVert + 3 is less than 271,
							sprite(1).locV = currVert + 3
							--move the arm down 3 pixels.
       updateStage                          --Update the stage to show changes.
    end if
  end repeat
end


					  

You have created and initialized the currVert variable by setting it to the value of sprite(1).locV and then replaced most of the occurrences of sprite(1).locV with the new variable. Notice that you cannot replace the occurrence of sprite(1).locV used to determine the sprite's vertical location.

7.
Close the Script window, save your work, and play the movie.

Everything works just as it did before. You have simply cleaned up the code by using a local variable.


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


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