Saturday, July 6, 2013

Difference in accessing MovieClips on TimeLine and in ActionScript




I am always curious about how things happen. In flash I had a confusion as there is difference in accessing MovieClips on Timeline and accessing MovieClips in ActionScript. So somewhere on web I found this answer. I’ve just copy and pasted it here. Thanks to person “x” who explained this.


In Flash 9 when you create MovieClip instance on the Timeline and give it an instance name, you are doing two things:
  1. You're assigning the name property of the MovieClip instance to be the string equivalent to the instance name provided
  2. You're creating a variable in the current timeline with the name of the instance name that references that MovieClip instance

Flash does this behind the scenes when you publish your SWF to help you manage your movie clips on the screen. It's important to note that this behavior (specifically #2) is not seen with ActionScript. For Example:
Code:
// my_mc is the instance name of a movie clip
// created on this timeline
trace(my_mc); // [object MovieClip]
trace(my_mc.name); // my_mc

// create a new movie clip via AS
// add it to my_mc
var another_mc:MovieClip = new MovieClip();
another_mc.name = "child_mc";
my_mc.addChild(another_mc);

// instance name not available in parent timeline
trace(another_mc);  // [object MovieClip]
trace(my_mc.child_mc); // undefined
trace(my_mc.another_mc); // undefined
You can see that neither the instance name (name property) nor the variable to which the new MovieClip created with ActionScript was assigned is referencable through the movie clip in which it was added (my_mc). This is because another_mc was created and added to the timeline dynamically. Flash will only save instance names as variables for movie clips created on the timeline in Flash.

If you want to use an instance name to get a MovieClip (or any DisplayObject) instance from the timeline in which it exists, you can use getChildByName();
Code:
trace(my_mc.getChildByName("child_mc"));  // [object MovieClip]);
This will work for all movie clips despite where or how they were located as long as they are within the timeline/movie clip from which getChildByName was used.


Enjoy ActionScripting.................

No comments:

Post a Comment