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

Lesson 21. Controlling Sprites with Ling... > Calling a Movie Handler from a Cast ...

Calling a Movie Handler from a Cast Member Script

Now you'll add the custom messages that will call the handlers you just added to the movie script.

1.
In the Cast window, click cast member 28 (the Up button). Then click the Script button to open the Script window.

You're going to add a line that calls the MoveArmUp handler you just created in the movie script.

2.
Replace the lines that move the arm up with the line shown here in bold:

on mouseDown
  repeat while the mouseDown       --While the mouse button is held down
    if rollOver(2) = TRUE then     --and the mouse is over the Up button (sprite 2),
       sprite(2).memberNum = 29    --display the highlighted button state.
       MoveArmUp( )
							--Call the MoveArmUp handler
							--in the movie script.
    else                           --Otherwise,
      sprite(2).memberNum = 28     --display the normal button state.
    end if
    updateStage                    --Update the stage to show changes.
  end repeat                       --The mouse button is no longer down,
  sprite(2).memberNum = 28         --so return to the normal button state.
  updateStage                      --Update the stage to show changes.
end


					  

Now when the Up button is clicked and the mouseDown handler runs, the handler calls the MoveArmUp handler in the movie script. After the MoveArmUp handler runs in the movie script, control returns to the mouseDown handler at the next line, which is else. However, because the if section of the handler has been executed, the else section of the handler is ignored. The next line to be executed, then, is the updateStage command. This redraws the stage to show the changes.

Now this cast member script contains only functionality for the button itself; it displays the highlighted or normal state of the button. The functionality that moves the arm up is in the movie script. If you ever need to modify the way the arm moves up, you can make the changes in the MoveArmUp handler in the movie script—that is, you don't need to search through the cast to find the button that contains that functionality; you need to look only in the movie script. (This advantage will become quite apparent when you begin building larger movies with numerous cast members.) Also, if other elements in your movie require the functionality contained in the MoveArmUp handler, you can use the handler again by calling it from another script. This practice of centralizing functionality in the movie script, where it is available to any handler, can save lots of maintenance time.

3.
Close the Script window.

Now you'll add a call to the MoveArmDown handler to the Down button.

4.
In the Cast window, click cast member 30. Then click the Script button to open the Script window.

You're going to add a similar line to the Down button.

5.
Replace the lines that moved the arm down with the lines shown here in bold:

on mouseDown
  repeat while the mouseDown          --While the mouse button is held down
    if rollOver(3) = TRUE then        --and the mouse is over the Down button (sprite 3),
       sprite(3).memberNum = 31       --display the highlighted button state.
       MoveArmDown()
							--Call the MoveArmDown handler
							--in the movie script.
    else                              --Otherwise,
       sprite(3).memberNum = 30       --display the normal button state.
    end if
    updateStage                       --Update the stage to show changes.
  end repeat                          --The mouse button is no longer down,
  sprite(3).memberNum = 30            --so return to the normal button state.
  updateStage                         --Update the stage to show changes.
end


					  

6.
Close the Script window and save your work.

Now you have a movie script that contains the MoveArmUp and MoveArmDown handlers. You also have a cast script attached to the Up button that calls the MoveArmUp handler in the movie script, and you have a similar cast script attached to the Down button that calls the MoveArmDown handler in the movie script.

7.
Rewind and play the movie. Click the Up and Down buttons. Stop the movie when you're done.

It still works!

There's no visible difference in the functionality of the buttons (well, almost none— more on this later). However, you have just made the handlers easier to read and more modular.


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


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