Eli Stone
Eli Stone

Reputation: 1555

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.

Upvotes: 1

Views: 2303

Answers (4)

Kabir
Kabir

Reputation: 1489

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;

Upvotes: 1

Eli Stone
Eli Stone

Reputation: 1555

Fixed.

I was being rava stupid as normal, forgot to put them inside the init :|

For the people who might come across this problem.
Working Code:

    public function Main()
    {
        // constructor code
        test.addEventListener(MouseEvent.CLICK, myButtonClick);

    }
        function myButtonClick(ev:MouseEvent):void
    {
        trace("button Clicked);
    }

Anyway thanks guys for the help sometimes its just the simplest answers are the correct ones.

Eli

Upvotes: 0

Simon McArdle
Simon McArdle

Reputation: 893

Have you assigned the instance of the button on the stage the name "test"? The error message you posted seems to say there is nothing with the name "test" to assign the event listener to.

To check, click on the button the stage and look at the 'Properties' tab: will show in a text box near the top if it needs assigning.

Now the second error you posted means you're referring to something called "myButtonClick" without first declaring/initialising a variable/function with that name. You will either need to declare it or correct it if you meant to refer to something else.

Upvotes: 0

Moorthy
Moorthy

Reputation: 754

Please go thru below and let me know which of the way you were having.

1) Are you having Document class? There is a field Class in the Document Properties under the Publish tab of the flash IDE, if you are giving your class name in that field then it is called as Document Class

If you are having document class then you can create listener to your button even in the constructor button. Flash won't throw any errors like you got.

2) If you are instantiated your class in the first frame, it won't have the properties of stage till you add that to the stage using addChild. Also it won't have access to your button. And so it will throw the error, The access of undefined property.

Upvotes: 0

Related Questions