Jets
Jets

Reputation: 3

ActionScript 3 Access Manually Added MovieClip Instance From A Class (not Document Class)

If I have an instance named "btnExit" which I added manually to the Main Timeline, I can refer to it from my Document Class simply by typing its name, eg.

package{
    public class Engine extends MovieClip{

        public function Engine(){
            trace(btnExit.x);
        }
    }
}

But now I have an instance that is nested in another movieclip, and I want to access it from a class (not a Document Class).

Let's say I have "Menu" movieclip on my library.

Then I manually drag a "ButtonExit" button to the "Menu" movieclip timeline, I named the instance "btnExit", so "btnExit" is the child of "Menu" movieclip.

The "Menu" movieclip will be added dynamically by code to the main timeline.

Now I want to access the "btnExit" from "Menu" class file, so I write these codes.

The Document Class:

package{
    public class Engine extends MovieClip{

        public var menu:Menu;

        public function Engine(){
            menu = new Menu();
            addChild(menu);
        }
    }
}

The Other Class:

package{
    public class Menu extends MovieClip{

        public function Menu(){
            trace(btnExit.x);
        }
    }
}

But I got error #1009 (null object reference) for unable to access btnExit;

Can anybody help me, pleaseee?

Upvotes: 0

Views: 771

Answers (1)

Snukus
Snukus

Reputation: 1302

Make sure that the instance of ButtonExit exists on every frame of the menu timeline and that it's instance named appropriately on every frame or write an if statement to only run your code if the Button is currently on the correct frame.

Every frame when the menu clip animates it's going to re instantiate everything on the frame, so if your second/third/fourth/etc frame doesn't have a clip named btnExit it's going to throw an error when the constructor is called for that frame.

Upvotes: 1

Related Questions