问题
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?
回答1:
var t:MovieClip = new MovieClip()
回答2:
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.
回答3:
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);
回答4:
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!
来源:https://stackoverflow.com/questions/3371495/accessing-movieclips-in-movieclips