4 Replies Latest reply on May 9, 2009 6:41 AM by Gregory Lafrance

    Returning a value from an eventhandler function

    jcahill23

      Hi.

      I have a simple event listener object.addEventListener(MouseEvent.CLICK, eventhandler) and I want the eventhandler function to return a value of type number but b/c the eventhandler is called from within the eventlistener how do I store the returned value in another variable?

       

      thanks

        • 1. Re: Returning a value from an eventhandler function
          Michael Borbor Level 4

          You could create a global variable and set the value inside the event 

          listener.

           

          Sincerely,

           

          Michael

           

          El 08/05/2009, a las 14:52, jcahill23 <forums@adobe.com> escribió:

           

          >

          Hi.

          I have a simple event listener 

          object.addEventListener(MouseEvent.CLICK, eventhandler) and I want 

          the eventhandler function to return a value of type number but b/c 

          the eventhandler is called from within the eventlistener how do I 

          store the returned value in another variable?

          >

          thanks

          >

          • 2. Re: Returning a value from an eventhandler function
            run,ryan! Level 3

            like Mike said, store it in global var

            or inline function if you like it

            object.addEventListener(MouseEvent.CLICK, function(e:MouseEvent):void{myReturnValue = eventhandler(e);})

            I don't like inline function

            • 3. Re: Returning a value from an eventhandler function
              jcahill23 Level 1

              Hi. Thanks for replying.

              I get what you mean by storing the returned value as a global variable but how do u set the value of the global variable inside the event listener? could you show what u mean exactly? thanx.

              • 4. Re: Returning a value from an eventhandler function
                Gregory Lafrance Level 6

                Here is an example showing how to set such as global variable from wihtin a mouse click or mouse over event handler.

                 

                If this post answers your question or helps, please mark it as such.

                 

                <?xml version="1.0"?>
                <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
                  <mx:Script>
                    <![CDATA[
                      [Bindable] private var myGlobalVar:String = "I have no value!!!";
                     
                      private function clickFunc(evt:MouseEvent):void{
                        myGlobalVar = "I was clicked!!!";
                      }
                      private function mouseOverFunc(evt:MouseEvent):void{
                        myGlobalVar = "Mouse was moved over me!!!";
                      }
                    ]]>
                  </mx:Script>
                  <mx:Button label="Click Me" click="clickFunc(event)"
                    mouseOver="mouseOverFunc(event)"/>
                  <mx:Text text="{myGlobalVar}"/>
                </mx:Application>