TARGETING THE CURRENT MOVIE


Whenever a script is placed in a timeline, such as on a frame or on a button, and the target for those actions is that same timeline, it can be said that the target for the actions in that script is the current movie. This is sort of like telling yourself to do something: For example, if you were to tell yourself to stand up, that action could be said to be targeting the "current" person you since you gave and responded to the command. (That is, you're both the commander and the person taking orders!) If you were to script this action, it would look something like the following:

 standUp(); 

Note that the action doesn't include a target path: This is because it targets the current movie. After all, if you were to tell yourself to stand up, you wouldn't normally address yourself prior to giving the command. In much the same way, Flash understands that when an action doesn't include a target path, it's meant to affect the timeline where the script resides.

Another way to address the current movie in a script is by preceding an action with the term this , as in the following:

 this.standUp(); 

In essence, you're saying, "With 'this' timeline, take the following action." We'll go into greater detail about this later in the book, but for the time being just remember that in all cases, the following are interchangeable when addressing the current movie:

 action ();  this.action (); 

Keep in mind that your project may contain many scripts on various timelines that don't contain target paths. When this is the case, each script affects its own timeline, or the current movie.

In the following exercise, we'll target the current movie for several timelines using no target path and the term this interchangeably. We use both because we want you to grow accustomed with the use of this as well as how it relates to the current referenced object because you will have opportunities to use it in more advanced ways later.

  1. Open currentTarget1.fla in the Lesson03/Assets folder.

    This project consists of a single scene with four layers, each named according to its content. The Background layer contains four keyframes: At each of these keyframes the background is a different color. (Moving the playhead back and forth will reveal this.) In the middle of the stage is a movie clip instance of a very rounded fellow.

    graphics/03fig05.gif

    In our first scripting task, we'll instruct the main timeline to move to a random frame when it begins playing, causing a random background color to appear. After that, we'll work with the movie clip instance in the middle of the stage, script it to do something, add a few family members, and then script them to do something as well.

  2. Select Frame 1 of the main timeline and add the following script:

     startingColor = random (4) + 1;  this.gotoAndStop (this.startingColor); 

    The first thing to be aware of is that the keyframe on Frame 1 of the main timeline is currently selected which means the main timeline is the current movie in the context of this script. Thus, any actions you enter in the Actions panel without a target path (or that use this) will affect this timeline.

    The first action generates a random number between 1 and 4, and assigns it to startingColor . By using random (4) we're asking Flash to generate a random number with four possible values, which always begin with zero that is, 0, 1, 2, or 3. By adding 1, as we have, the possible values generated become 1, 2, 3, or 4 necessary because that number will be used next to tell the current movie to go to and stop at a frame number based on the value of startingColor (and timelines don't have a Frame 0). This will result in the random display of one of the background colors when the movie begins playing.

    Notice also that no target path is used in assigning the value of startingColor ; however, this is used with the variable in the next action: They mean the same thing and are thus interchangeable.

  3. Double-click the movie clip instance in the middle of the stage to edit it in place. Since you'll need a solid understanding of this movie clip's timeline to complete several of the exercises in this lesson, let's take a minute now to get acquainted with it.

    This timeline has two frame labels: Quiet and Speak. At the Speak label a text balloon appears that includes a text field named balloon. We will soon create a script that displays text within this text field whenever the timeline is moved to the Speak label.

    graphics/03fig06.gif

    The Button layer of this timeline consists of an invisible button that's placed on top of all of the graphics on this timeline. We will script this button in the next step.

    Finally, Frame 1 of this timeline's Actions layer contains a stop() action to prevent the timeline from moving past this frame until we instruct it to.

  4. With the Actions panel open, select the invisible button and add this script:

     on (press) {    startDrag (this);    gotoAndStop ("Speak");    balloon.text = words;  }  on (release) {    stopDrag ();    this.gotoAndStop ("Quiet");  } 

    The first set of actions is executed when this button is pressed, causing the movie clip instance to become draggable. Next, the same timeline is sent to the frame labeled Speak. In addition, we set the text to be displayed in the balloon text field to the value of words the name of a variable that will contain a string of text. We will define this variable shortly.

    As the script shows, when the button is released, dragging stops and the timeline is sent to the frame labeled Quiet.

  5. Return to the main timeline. With the Library panel open, drag a couple more instances of the Hatfield movie clip onto the stage. Choose Control > Test Movie to test what you've done so far.

    The first thing you'll notice is that the main timeline goes to a random frame to display one of the background colors on Frames 1 through 4.

    Press any of the instances, and you'll notice that they've become draggable and that their text balloons appear. The concept to grasp here is that the invisible button that enables this functionality is part of the master movie clip itself. Thus, each instance of the clip contains this button as well as the script that makes it work. The script on the button is set up to make the timeline it is part of (the current movie) draggable. Since each instance is considered a separate timeline, only the instance that contains the currently pressed button will be dragged when the button is pressed. When actions are placed on a movie clip's timeline or a button inside the movie clip, and those actions target its own timeline, each instance of the movie clip inherits that scripted functionality. Think of this as genetically programming instances that are based on the same master movie clip so they all have a fundamental function.

    Using clip event handlers, we can give each instance its own personality, on top of what it is "genetically" programmed to do. Let's look at that next.

  6. Close the test window to return to the authoring environment. With the Actions panel open, select one of the three instances on the stage and add this script:

     onClipEvent (load) {    words = "My name is Derek";    this._xscale = 75;    _yscale = 75;  } 

    As you learned in the previous lesson, clip events (load , enterFrame , mouseMove , and so on) are event handlers attached to movie clip instances. As such, they allow you to script actions that affect single movie clip instances (rather than every instance of a movie clip). Thus, you would use clip events outside the movie clip's timeline to script characteristics unique to specific movie clip instances, but you would script shared traits inside the movie clip's timeline (on a frame or button inside the clip) so that all instances of that movie clip would inherit those characteristics. Both sets of actions target the current timeline but with a different scope.

    If you were to extend this metaphor to people, you could think of shared traits (the things you script inside your movie clip's timeline) as humans' common capacity to think, feel, and move things we all inherit. Unique characteristics (the things you script outside your movie clip's timeline), on the other hand, could include name, size, and location things that are individual to each of us.

    graphics/03fig07.gif

    The script above is executed when the instance loads, or first appears in the movie. The first action assigns a text value to words . When this action is executed, both the name and value of this variable are stored in this instance. You'll also remember that the value of this variable is used to set the text that is displayed in the balloon text field mentioned in Step 4. The next two actions scale the instance by 75 percent.

  7. With the Actions panel open, select one of the other two instances on the stage and add this script:

     onClipEvent (load) {    words = "My name is Ashlie";    _x = 400;    _y = 300;  } 

    This script is similar to the previous one except that it sets the value of words to "My name is Ashlie" and, on loading, moves this instance to a location 400 pixels horizontally from the left of the stage and 300 pixels vertically from the top of the stage.

  8. With the Actions panel open, select the last instance on the stage and add this script:

     onClipEvent (load) {    words = "My name is Kathy";  }  onClipEvent (mouseMove) {    this._rotation = this._rotation + .5;  } 

    When this instance is loaded, the first action will set the value of words to "My name is Kathy." A second clip event rotates the instance half a degree each time the mouse is moved.

  9. Choose Control > Test Movie to see what we've done so far.

    On loading, you'll see that our scripts have caused several things to occur: The first instance we scripted is now 75 percent smaller than the other two. The second instance we scripted can be seen at the X and Y coordinates we told it to go to on loading. And the third instance rotates as the mouse is moved. Press any of these movie clip instances, and you'll see that you can still drag as before but that the text balloon now includes text customized for each instance. Once again, remember that while scripts placed inside the movie clip they target will affect all instances of that clip, clip events (attached to individual instances) let you customize each instance.

  10. Close the testing environment to return to the authoring environment. Save this file as currentTarget2.fla.

    We will build on this file (with a few modifications) in the following exercise.



Macromedia Flash MX ActionScripting Advanced. Training from the Source
Macromedia Flash MX ActionScripting: Advanced Training from the Source
ISBN: 0201770229
EAN: 2147483647
Year: 2002
Pages: 161

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net