Copy link to clipboard
Copied
I'm fairly new to AS3 and am having a hard time targeting a clip from within a nested clip. I am following instructions from books and advice on the web but keep getting errors. Very frustrating.
This is how the nested clips are set up:
Main timeline > MC1
> mc1a
> mc1b
> mc1c
I want to get the height of mc1a from the timeline of mc1b and then change the height of mc1c with that information.
The book says to declare the main timeline as a display object
var myRoot:MovieClip = root as MovieClip;
So now should I be able to do this from the timeline of mc1b? :
trace(myRoot.MC1.mc1a.height)
I get this error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
This is harder than it needs to be. With AS2 I could just use _root.MC1.mc1a.
I'm guessing that since these are compiler errors, the objects showing as Null haven't loaded yet. Since I had the trace call on the first frame of movie clip it runs the code as soon as it loads and I get an error. But if I place the code later in the timeline and navigate to it, it works because the whole movie by then has loaded. Just a guess.
Copy link to clipboard
Copied
Have you tried using: "trace(MovieClip(root).MC1.mc1a.height)" within the mc1b object?
Copy link to clipboard
Copied
It comes up as Null. The actual instance name of an object I want to target is scrollerMC. This clip is on the main time line. Here are the results I get when I try to target it from the time line of a nested clip using different methods:
import flash.display.MovieClip;
var mcRoot:MovieClip =root as MovieClip;
trace(mcRoot.scrollerMC.height);
trace(MovieClip(root).scrollerMC.height);
The code above produces TypeError: Error #1009: Cannot access a property or method of a null object reference.
trace(MovieClip(root).getChildByName("scrollerMC").height);
This last one works. I don't understand why the first two attempts don't work. My expensive Actionscript 3 book and other web posts say it should. I would prefer to use the shorter code.
Copy link to clipboard
Copied
If getChildByName works then my best guess is that you have created the object dynamically rather than placed it on stage manually. The "name" property (a string) is not the same as the instance name, unless you manually created the object and placed it on the stage and assigned it an instance name using the Properties panel. The name property is simply a label of sorts, but you can use the getChildByName method to target an object using its name property.
Copy link to clipboard
Copied
The object was created manually and give an instance name in the Properties panel.
More wierdness is happening. This works in he nested clip when I jump to a frame in the timeline. But I get the Error #1009 when I place it on frame 1 of the same time line.
trace(MovieClip(root).scrollerMC.contentMask.height);
I feel I'm wasting a lot of time just trying to figure out how to target movie clips, and when I find something that does work it no longer works when I try to use the code somewhere else in the same time line. Documentation is ambiguous about the correct way to do it.
Copy link to clipboard
Copied
I'm guessing that since these are compiler errors, the objects showing as Null haven't loaded yet. Since I had the trace call on the first frame of movie clip it runs the code as soon as it loads and I get an error. But if I place the code later in the timeline and navigate to it, it works because the whole movie by then has loaded. Just a guess.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now