AS3: Access button from class

So im quite new to AS3 but have worked with AS2 a lot before. I have created a button and placed it on my stage then inside my class i have added this:

    test.addEventListener(MouseEvent.CLICK, buttonClicked);
    function buttonClicked(ev:MouseEvent):void
    {
        trace("Clicked");
    }

Now this does not work as it can't find the stage, the only way i can get this to work is if i put the listener on the same frame as the button & not in the class.

But there must be away around this.

Thank you. Eli

Update - adding Error messages

If I keep the above code all in the external class these are the errors i get.

Line 22 1120: Access of undefined property test. Line 22 1120: Access of undefined property myButtonClick.

Answers


If you have created a document class with timeline then your "test" button must be in first frame. Because document class starts executing from first frame. You can access your button instance only when its available in stage.

Oh, I forgot to mention. You have to declare those instances as public variable in your document class.

public var test:SimpleButton;

Need Your Help


About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.