views:

43

answers:

4

I am starting with AS3 programming icm with Flash Builder 4.

I have created a "Main" class which is the initial stage.

On this stage there is a movieclip called mcMain.

In this mcMain is 4 other movieclips: mcStats1 mcStats2 mcStats3 mcStats4

How can I access these mcStats1-4 from the code to make them go to another frame?

I tried:

var t:MovieClip = MovieClip(getChildByName("mcMain"));
t.getChildByName("mcStats1").gotoAndPlay(3);

But that just keeps giving me null reference errors: TypeError: Error #1009: Cannot access a property or method of a null object reference.

So how should I solve this?

A: 

var t:MovieClip = new MovieClip()

Hwang
and then?Can't do var t:MovieClip = new MovieClip(getChildByName("mcMain"));
Rogier21
A: 

Assign an instance name to the mcMain movie clip. Now if this mcMain has a corresponding actionscript class, declare four variables (or an array) in it and store references to mcStats clips (their respective instance names) in them. If it doesn't have a class, you can create properties on the mcMain object itself (since MovieClip is a dynamic class) and store the instance names of stats clips in there.

This way, you can access them as mcMain.mcStats1 or mcMain.statArray[0] etc.

Amarghosh
I am not following you.I do have a class for the mcStats1, but how can I access them? mcMain is undefined in the AS3 code, while I did give it that instance name.
Rogier21
A: 

I figured it out myself, it wasn't easy but apperantly typecasting is the answer in AS3.

var p1:mcUserStats; //class name
p1 = ((this.mcMain as MovieClip).mcStats1 as mcUserStats);
Rogier21
A: 

Just another note, it is important to turn off "Automatic declare instances" in AS3 if you want to be able to do this:

Create a MovieClip name "test" and drag it on the stage. In the header the class of the stage add: public var test:Movieclip;

Now you can access the object in the class!

Rogier21