Targeting an instance

If you want something to work in your SWF file, you need to target that instance and then tell it to do something, such as assigning it an action or changing its properties. You usually need to define where that instance is in the SWF file (for example, what timeline it's on or what instance it's nested within) by creating the target path. Remember that you have given many of the instances in your FLA file instance names, and then you added code to the FLA file that uses those instance names. When you do this, you target that particular instance and then tell it to do something (such as move the playhead or open a web page). For more information on objects and properties, see Object data type.

To target an instance:

  1. Select File > New and select Flash Document.
  2. Select File > Save As and name the file target.fla.
  3. Use the Oval tool to draw a shape on the Stage. Draw an oval of any size and color.
  4. Use the Selection tool to select the oval on the Stage.

    TIP

    Remember to select the stroke and fill if necessary.

  5. Select Modify > Convert to Symbol, select the Movie Clip option, and then click OK to create the symbol.
  6. Select the movie clip on the Stage and give it the instance name myClip in the Property inspector.
  7. Insert a new layer and rename the layer actions.
  8. Add the following ActionScript to Frame 1 of the actions layer:
    myClip._xscale = 50;
    

    This line of code targets the myClip instance on the Stage. The ActionScript scales the instance to half its original width. Because the ActionScript is on the same timeline as the movie clip symbol, you only need to target the instance using the instance name. If the instance was on a different timeline or nested within another instance, you would need to modify the target path accordingly.

Targeting a nested instance

You can also target instances that are nested inside other instances. Perhaps you want to place a second movie clip instance inside of the myClip instance from the exercise in Targeting an instance. You can also target that nested instance using ActionScript. Before you proceed with the following exercise, you need to complete the exercise in Targeting an instance, and then follow these steps to target a nested instance.

To target a nested instance:

  1. Open target.fla from the procedure on targeting an instance, and rename it target2.fla.
  2. Double-click the myClip instance on the Stage.
  3. Select the Oval tool and draw another oval inside of the myClip instance.
  4. Select the new shape, and then select Modify > Convert to Symbol.
  5. Select the Movie Clip option and click OK.
  6. Select the new instance, and type myOtherClip in the Instance Name text box of the Property inspector.
  7. Click Scene 1 in the edit bar to return to the main Timeline.
  8. Add the following ActionScript to Frame 1 of the actions layer:
    myClip.myOtherClip._xscale = 50; 
    

    This ActionScript resizes the myOtherClip instance to 50% of its current width. Because the target.fla file modified the myClip instances _xscale property, and the myOtherClip is a nested symbol, you'll notice that myOtherClip will be 25 percent of the original width.

If you work with nested movie clips that have their own timelines, you can manipulate the playhead in a nested instance's timeline using code similar to the following snippet:

myClip.nestedClip.gotoAndPlay(15);
myClip.someOtherClip.gotoAndStop("tweenIn");

Notice that the clip that you manipulate (such as nestedClip) appears right before the action. You'll notice this trend in upcoming sections.

You aren't limited to accessing predefined methods and properties of instances on the Stage, as demonstrated in the previous examples. You can also set a variable within a movie clip, as seen in the following code, which sets a variable in the starClip movie clip:

starClip.speed = 1.1;
starClip.gravity = 0.8;

If either the speed or gravity variables existed previously in the starClip movie clip instance, the previous values would have been overwritten as soon as the new values were set. You are able to add new properties to the starClip movie clip, because the MovieClip class was defined with the dynamic keyword. The dynamic keyword specifies that objects based on the specified class (in this case MovieClip) can add and access dynamic properties at runtime. For more information about the dynamic statement, see dynamic statement in the ActionScript 2.0 Language Reference.