views:

718

answers:

5

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 error:

1120: Access of undefined property myBtn.

A: 

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;
JBRWilkinson
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.
Wes
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.
JBRWilkinson
Okay, that makes sense. I've:1. Added the button to the components2. Dragged it onto my Stage3. Gave it an instance name of 'myBtn'4. Inserted the above code into actions frame 1I still receive Error 1119
Wes
Name the Component/Library item something different to the instance name - which name is it complaining about?
JBRWilkinson
A: 

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.

Ipsquiggle
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)
Allan
+1  A: 

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.

Josh
+1  A: 

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.

Ipsquiggle
A: 

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