How to trigger a button click in AS3

I know you could do this vary easy in AS2 just by calling the myButton.onClick(); function. But, we all know with AS3, there is much more code to write.

This will give you a trace of “hi world” just as if you clicked the button in actionscript 3:

myButton.addEventListener(MouseEvent.CLICK, onClick);
myButton.myVar = "hi world";
 
function onClick(e:Event = null):void {
trace(e.target.myVar)
}
 
myButton.dispatchEvent(new MouseEvent(MouseEvent.CLICK));

You may also like...

Leave a Reply