Posts Tagged ‘event listener’

Adding an event listener function with arguments using Actionscript

Monday, August 31st, 2009

This might come handy whenever you can’t use directly mxml and you need to add an event listener function that needs one or more arguments.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="myInitialize()">
 <mx:Script>
  <![CDATA[
   import mx.controls.Alert;

      private function myFunction(arg:String):void {
       mx.controls.Alert.show("Received call with argument=" + arg);
      }

   private function myInitialize():void {
    myButton.addEventListener(MouseEvent.CLICK, function(event:MouseEvent):void{
     var someArg:String="an_argument";
     myFunction(someArg);
    });
   }
  ]]>
 </mx:Script>

 <mx:Button id="myButton" label="Click me!"/>
</mx:Application>

See this code in action here