Learning ActionScript 2.0 in Adobe Flash |
|
|
|
| Syntax and Language Fundamentals > About dot syntax and target paths > About using dot syntax to target an instance > 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:|
TIP |
Remember to select the stroke and fill if necessary. |
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.
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: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.
|
|
|
|