Hide a Button in AS3?

wesbos picture wesbos · Jan 13, 2010 · Viewed 23.8k times · Source

I have a button called myBtn.

In my actions in Frame 1, I have tried both:

myBtn.visibility = false;

myBtn.enabled = false;

Both give me the same error:

1120: Access of undefined property myBtn.

Answer

Allan picture Allan · Jan 14, 2010

ok. There are a couple of ways that you can do this. The first just involves using the timeline.

Method 1 - Timeline

Step 1. Go to Window tab, then select components. Drag a Button instance onto the stage.

Step 2. In the properties panel, where it says 'Instance Name', replace with "myBtn" (make sure you don't use the quotes :P)

Step 3. On the timeline enter this code in frame 1.

myBtn.visible = false;

Method 2 - Document Class

Step 1. Place an instance on the stage as in the timeline

Step 2. Create a class, lets call it Resource.

Step 3. add

import flash.display.SimpleButton; 

Step 4. Create a public static member

public static var BTN_MY_BUTTON:SimpleButton;

Step 5. In your document class add this to the contstructor.

addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true);

Step 6. Add this function

private function init(e:Event):void

 Resource.BTN_MY_BUTTON = myBtn;
}

Step 7. Now in any class you can access the button by going

Resource.BTN_MY_BUTTON.visible = false;