How can I check component (move clip or button) isInitilazed
before add event (release or click) for it in actionscript 3?
views:
322answers:
2May I inquire what it is that you're trying to do, in more general terms?
If you just want to avoid null reference errors, just check the reference (variable) you're using for null
:
if (_myComponent != null)
{
// add listeners
}
If for some other reason you really need to know if the component has been initialized before you do this, you need it to dispatch some kind of event for this (Flex's UIComponents
dispatch FlexEvent.INITIALIZE
events, but I assume you're not using Flex) or alternatively set an "initialized" property, which I'm not so sure the standard Flash components do (I'm looking at the Button
documentation for reference here.) You could of course make a custom subclass of whichever component you're using for yourself where you implement an initialized
property, but I'm not sure how you could implement this for any of the standard Flash components. I am also not quite sure what you mean by "initialization" in this context :)
Maybe something like this?
if (mcWithComponents.stage) doStuff();
mcWithComponents.addEventListener(Event.ADDED_TO_STAGE, doStuff);
private function doStuff(e:Event = null):void
{
mcWithComponents.myButton.addEventListener(MouseEvent.CLICK, buttonClicked);
}
Or add the listener directly in the class of the movieclip.
public function mcWithComponents() // constructor
{
if (stage) init();
addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
myButton.addEventListener(MouseEvent.CLICK, buttonClicked);
}