flashactionscript-3actionscriptadobe-flash-cs3

Hide a Button in AS3?


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.


Solution

  • 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;