Controlling Flash Professional Library Components in a .as file
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 w开发者_开发技巧hen I run it.
Can anyone explain the workflow to make a Flash library component accessible in a .as class file using Flash CS5? Thanks.
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);
}
}
}
精彩评论