Hide a Button in AS3?

◇◆丶佛笑我妖孽 提交于 2019-12-01 20:55:49

问题


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.


回答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;



回答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.




回答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;



回答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.




回答5:


You're getting this error because the AS3 compiler can't resolve the name 'myBtn'.

What is 'myBtn'? is it a Symbol, Component or named instance of either?

Once you've clarified that, the following code will do the job:

myBtn.visible = false;



回答6:


If you are very interested in accessing the button from the timeline, I recommend sticking with AS2, as it greatly simplifies this process. Many scenarios involving accessing instances placed in the editor, running code from the timeline, and so forth, become less trivial in AS3.

If you want to stick with AS3, then instead of putting your code in frame 1, you should create a document class, and from there you can declare the objects that you have on the stage, which simplifies accessing them.



来源:https://stackoverflow.com/questions/2058503/hide-a-button-in-as3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!