TARGETING MOVIE CLIP INSTANCES ON LEVELS


Targeting a movie clip instance that exists within a SWF loaded into a level is a straightforward process: Simply input the level number of the SWF that holds it, followed by the instance name itself. An instance called cuteDog within a SWF loaded on Level 42, for example, would have the following target path (relative to movies on other levels):

 _level42.cuteDog 

If cuteDog itself contained a movie clip instance named tail, that instance's target path would be as follows:

 _level42.cuteDog.tail 

Although we've used absolute paths in the above (since we're targeting timelines on different levels), remember that you can use relative target paths to target timelines on the same level.

In this exercise we'll build on our operating system project by targeting movie clip instances in SWFs loaded into levels as well as show you how to use simple data from one level in another.

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

    This is the same file that we added scripts to in the last exercise with one addition: The timeline now includes a layer called Color Clip.

  2. Press the Show Layer button (with the big red X) on the Colors Clip layer to reveal the content of this layer.

    graphics/03fig17.gif

    You should now see a purple box that covers the entire stage. This box is a movie clip instance named colors. This movie clip instance will act as a color layer above the multicolored textured background you see when this instance is not visible. Changing the color and transparency of this instance will make it seem as if the textured background on the desktop is changing. Soon we'll script the movie that's loaded into Level 1 (backgroundControl.swf) to change colors and control transparency of this instance.

  3. Double-click the colors movie clip instance in order to edit it in place.

    The timeline of the colors movie clip instance now appears. This timeline is made up of a single layer that contains several frame labels. At each of these frame labels, the colored box on stage is filled with the color associated with that frame label's name. We will control this clip's color by sending it to different labels on its timeline. Initially, the clip will be purple because this is the color at Frame 1, and that frame includes a stop() action to prevent the timeline from moving until we instruct it to.

    graphics/03fig18.gif

  4. Return to the main timeline. With the Property inspector open, select the text field in the lower left portion of the stage, next to where it says "Enter Text:"

    Looking at the Property inspector, you'll notice that this is an Input text field named inputText. The user can enter any text he or she wishes here, and that text will be used in the movie loaded into Level 2 (textBox.swf). Next, we'll set up the script that creates this functionality.

  5. With the Actions panel open, select the button on the desktop that resembles a scroll of paper and add the following line of script just below _level2._visible = true; :

     _level2.inputText.text = _level0.inputText.text; 

    There is a text field in the movie that gets loaded into level 2 that is named inputText, which is the same name as the text field in this movie, as we discussed in the previous step. When this button is pressed, this action will set the value of the text field on Level 2 to the same value as the text field in this movie. This is a simple example of how data can be transferred between timelines.

  6. With the Actions panel open, select Frame 1 of the main timeline and add the following line of script just below where it says loadMovieNum ("textBox.swf", 2); :

     _level0.colors._alpha = 50; 

    This action will make the colors movie clip instance (which resides on the Colors Clip layer) 50 percent transparent when the movie begins to play. We used a target path of level0 for this action even though it's not required to reinforce your understanding that the first movie to load (this movie) is automatically loaded into Level 0.

  7. Export this file to a SWF and name it levelTarget.swf in the Lesson03/Assets folder.

    We will play this file at the end of this exercise.

  8. Save this file as levelTarget3.fla.

    We're finished scripting this file.

  9. Open backgroundControl2.fla in the Lesson03/Assets folder.

    This is the same file we used in the last exercise the one that gets loaded into Level 1 of our project. In the previous exercise we scripted it to become invisible on loading, to close when the Exit button is pressed, and to become draggable when the invisible button at the top of the box is pressed. Here, we'll enhance its functionality by enabling it to control the colors movie clip instance that resides on Level 0. We'll attach scripts to the many square buttons that you see.

    Before we begin scripting, you need to familiarize yourself with several of the elements on the stage. On the left, just below the word Transparency, you'll see a dynamic text field named alphaAmount. You will use it in two ways: to display the current transparency of the colors movie clip instance on Level 0, and to display the amount of transparency that will be applied to that instance when any of the five Transparency buttons are rolled over. To the left of this text field are five square buttons that will be scripted to change the transparency of the colors movie clip instance. Below these buttons are nine additional buttons that resemble color swatches. These will be set up to send the colors movie clip (Level 0) to the various frame labels on its own timeline. Below these buttons is a movie clip instance named currentColor, which is essentially a smaller version of the colors movie clip instance. Its timeline has similar color boxes, as well a frame labels as the colors movie clip instance. This movie clip instance will be used to display the current color applied over the textured background on the desktop (Level 0). Below this movie clip instance is another text field, colorName, which displays the name of the color as the mouse rolls over the color swatches above it.

    graphics/03fig19.gif

  10. With the Actions panel open, select the square button farthest left from the alphaAmount text field and add this script:

     on (rollOver) {    alphaAmount.text = 0;  }  on (release) {    _level0.colors._alpha = 0;  }  on (release, rollOut) {    alphaAmount.text = _level0.colors._alpha;  } 

    This button will be used to set the transparency of the colors movie clip instance on Level 0 to 0.

    The first action will display 0 in the alphaAmount text field when the button is rolled over. This is simply to provide the user feedback about what amount of transparency this button will apply. With the next action, when the button is released, the alpha property of the colors movie clip instance is set to zero, which makes it transparent. (Note the simplicity of this target path: level number followed by instance name.) The next action will set the value displayed in the alphaAmount text field to the current transparency of the colors movie clip instance when the button is released or rolled away from.

    You can attach the same script to the buttons to the right of this one and simply replace the 0 values with 25, 50, 75, and 100, respectively.

  11. With the Actions panel open, select the purple Color Swatch button (the first one on the left) and add this script:

     on (rollOver) {    colorName.text = "Purple";  }  on (release) {    currentColor.gotoAndStop ("Purple");    _level0.colors.gotoAndStop ("Purple");  }  on (release, rollOut) {    colorName.text = "Please choose a color";  } 

    This button will set the color of the colors movie clip instance on Level 0 by moving that timeline to the appropriate frame label.

    The first action will display "Purple" in the colorName text field when the button is rolled over to tell the user what color tint this button will apply. When the button is released, two actions are executed: The first sends the currentColor movie clip instance to the frame labeled Purple. This will change the box below the Color Swatch buttons to purple, indicating that this is the current tint. The next action sends the colors movie clip instance on Level 0 to the frame labeled Purple, causing the textured background on Level 0 to take on a purple tint (depending on its current transparency). The last action will reset the text in the colorName text field to read "Please choose a color."

    You can attach this same script to the buttons to the left of this one, simply replacing the three areas that have "Purple" values with "Maroon," "Lavender," "SkyBlue," "DeepBlue," "Orange," "GrassGreen," "SeaGreen," and "Pink," respectively.

    graphics/03fig20.gif

  12. Export this movie as backgroundControl.swf in the Lesson03/Assets folder.

    This creates a SWF from our project, which will be loaded into Level 1, overwriting a previously saved version from the last exercise.

  13. Save your work as backgroundControl3.fla.

    We're now finished working with this file.

  14. Locate the levelTarget.swf file that you created. Double-click it to open and play it.

    As soon as this file begins to play, backgroundControl.swf is loaded into Level 1 and textBox.swf is loaded into Level 2 both of which are made invisible on loading. The colors movie clip instance that overlays the textured background is currently tinted purple and has a transparency of 50 percent.

    Enter any text in the text field at the bottom of the screen, then press the button on the screen that resembles a scroll of text. This button was set up to make Level 2 visible as well as to feed this text to a text field on that level.

    Now press the button resembling a computer monitor to make the movie on Level 1 visible. This movie controls the color and transparency of the colors movie clip instance on Level 0. Press the Transparency and Color buttons in this movie to see how they affect that movie clip instance.

POWER TIP

As discussed at the beginning of this lesson, timelines can contain their own data, functions, and objects. While we haven't touched on these yet, it would still be prudent to briefly discuss a few points about how target paths relate to these dynamic elements, since most projects are set up so that a dynamic element on one timeline can be used by another.

For example, a variable may be named myVariable . If that variable exists in a movie clip instance named myMovieClip which itself is inside a movie loaded into level 74, then to use this variable in any of your scripts on any timeline, you would simply place the appropriate target path in front of the variable name, such as:

 _level27.displayText =_level74.myMovieClip.myVariable; 

The same thing applies to function calls:

 _root.anotherMovieClip.myFunction(); 

Or any type of object:

 _parent.myObject 

The importance of this will become more evident as we progress through the lessons in this book.




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