Hide a Button in AS3?

23,763

Solution 1

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;

Solution 2

If you do currently have a document class, then any instances placed on the stage need to be declared in the document class.

For example: If you put an object of class Button on the stage, and call it myBtn, then your document class needs to look like this:

package {
  import flash.display.MovieClip;
  import flash.display.Button;

  public class DocClass extends MovieClip {
    public var myBtn:Button;  // !!! This is the line that lets you access the instance

    public function DocClass() {
      //..
    }
  }
}

Otherwise, the combination of having a doc class but not declaring the instance will give you that 1120 error.

Solution 3

1120: Access of undefined property myBtn.

It does mean that your button has no name. You must to give the name for you button in properties panel of your button, in field (Instance Name) you will put the name myBtn and in actions write:

myBtn.visible=true; // visible button

or

myBtn.visible=false; // or to make your button invisible;

Solution 4

if you've got a button in the library you can add a new instance to the stage using the following:

import fl.controls.Button;

var myBtn:Button = new Button();
addChild(myBtn);

You can then reference it and hide it like this:

myBtn.visible = false;

It would be better to do this in a separate class as @Ipsquiggle suggested.

Share:
23,763

Related videos on Youtube

wesbos
Author by

wesbos

Author of Sublime Text Book HTML5, JavaScript, Sublime Text, Node.js, CSS3 and all good things.

Updated on January 20, 2020

Comments

  • wesbos
    wesbos over 4 years

    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.

  • wesbos
    wesbos over 14 years
    I've dragged it into 'components' and set it for use in Action script. I now get this error: 1119: Access of possibly undefined property visible through a reference with static type Class.
  • JBRWilkinson
    JBRWilkinson over 14 years
    So you've now got a Component named 'myBtn' in your Library. Did you drag your component back onto the Stage yet? You need to do that. Use the Properties pane to set an instance name. That's the magic that your AS code needs.
  • wesbos
    wesbos over 14 years
    Okay, that makes sense. I've: 1. Added the button to the components 2. Dragged it onto my Stage 3. Gave it an instance name of 'myBtn' 4. Inserted the above code into actions frame 1 I still receive Error 1119
  • JBRWilkinson
    JBRWilkinson over 14 years
    Name the Component/Library item something different to the instance name - which name is it complaining about?
  • Allan
    Allan over 14 years
    There is really no reason to use AS2. If anything its more complicated as you need to use delegates to refer to class members properly (or resort to hacks like declaring a member thisObj and assigning 'this' to it)

Related