views:

30

answers:

1

Hello,

I'm trying to control the behavior of components in my Flash Library using a .as file class, but it doesn't seem to be working.

For example, if I have a Button in my library called exampleBtn and attempt to do this in the .as file:

exampleBtn.visible = false;

... nothing happens. The button is still visible in my application when I run it.

Can anyone explain the workflow to make a Flash library component accessible in a .as class file using Flash CS5? Thanks.

+1  A: 

you need to give your assets instance names in order to control them. on your stage, select your exampleBtn instance and give it a name "exampleBtn" in the properties panel. however, it's best to give your instances a different name than its class name for reusability.

another cause of this problem is that your document class file is not being called by your .fla. select your stage and enter the document class name in the properties panel.

example: your document class file is called DocumentClass.as, so you enter "DocumentClass" in the class field in the properties panel. if DocumentClass.as in in a folder called Classes that is in the same directory of your .fla, the class filed in your properties panel will be "Classes.DocumentClass", and your .as package header will be "package Classes" instead of just "package"

make sure your document class file is saved and in the appropriate location before debugging.

finally, the problem might arrise simply because your stage hasn't initialized while you're calling your assets. this requires you to add an ADDED_TO_STAGE event listener, calling your assets afterward.

package Classes
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent; //import MouseEvent

public class DocumentClass extends Sprite
    {
    public function DocumentClass()
        {
        addEventListener(Event.ADDED_TO_STAGE, init);
        }

    private function init(evt:Event):void
        {
        removeEventListener(Event.ADDED_TO_STAGE, init);
        //exampleButton.visible = false;
        exampleButton.addEventListener(MouseEvent.CLICK, clickEventHandler);
        }

    private function clickEventHandler(evt:MouseEvent):void
        {
        trace(evt.currentTarget.name + " Clicked.  Event Details: " + evt);
        }
    }
}
TheDarkInI1978
Thanks for the response! This helped when it comes to setting properties like exampleBtn.visible = false, however, it doesn't seem to let me use an event listener with the exampleBtn. I can click the button (which should trigger the event listener with MouseEvent.CLICK), but nothing actually happens when I click it (e.g. the function isn't being called). I know the function isn't being called because when I put a trace() statement at the top of the function, it doesn't appear in the Output. Any ideas why this is?
rottendevice
if your class extends Sprite (and not a button), you have to add the event listener to the button instance. i've edited my original post with an example.
TheDarkInI1978
Thanks for the new response. I figured out my problem. I was trying to call my class name to trigger the event listener when I should have been using the instance name. Beginner's mistake. Thanks again for the help!
rottendevice