2 Replies Latest reply on Aug 29, 2006 3:00 PM by peterent

    ? Derive a clicked button's 'level' and instance  name?

    optimalweb
      I am trying to do something that I did quite often in Flash. When a button is clicked, I need to understand it's instance name and heirarchy as the application would identify it when looping through the DisplayList.

      For example, if a button, 'myButton', is clicked on in the application ' myApp', I need to get its programatic name as referenced by the application with 'myApp0.myButton'.

      In Flash, with AS2, I could have the button tell me its programatic name with:

      myButton.onRelease=function(){
      trace(this);
      }

      The result in Flash would be '_level0.myButton';

      How can I get this type of informtion with Flex? Is it different with MXML and AS3?

      For reference, I am using a similar function to that below to 'index' the list of objects in Application.application:

      function traceDisplayList(container:DisplayObjectContainer, indentString:String = ""):void {
      var child:DisplayObject;
      for (var i:uint=0; i < container.numChildren; i++) {
      child = container.getChildAt(i);
      trace (indentString, child, child.name);
      if (container.getChildAt(i) is DisplayObjectContainer) {
      traceDisplayList(DisplayObjectContainer(child), indentString + " ")
      }
      }
      }

      Any information and examples would be appreciated.

      Thank you.
        • 1. Re: ? Derive a clicked button's 'level' and instance  name?
          optimalweb Level 1
          Ok, I found a partial solution, but I could still use some help with event listeners. The solution I found is:

          Within Flex, on a button click, the application can list the click target when an event is called. So, when a user clicks a button, in the 'click' action of the button, the following code will return the object information I was looking for.

          <mx:Button label="myButton" click="trace(event.currentTarget)"/>

          Now I am interested in seeing if I can dynamically "listen" for clicks without adding an event command to the buttons. For instance, on the button above, I can have the button tell me its name, as shown above, or I could listen for the click and derive it. For example:

          <mx:Button label="myButton" click="trace(event.currentTarget); sayName(event)"/>

          Then I can have AS code listening for that event:

          private function sayName(event:MouseEvent):void {
          trace(event.currentTarget);
          }

          The problem with the listener solution 'sayName', is that it requires a button to pass the event to 'sayName()'. I am really looking for a solution which listens for ANY button click, checks to see if it is a button, or a text link, or any object that might act as a button (as opposed to a graphic or movie without 'semantic events' with the application performing a resulting action).

          So this is what I have tried:

          In an AS Class, I added the following to the main class function to initialize it:

          addEventListener(MouseEvent.CLICK, clickHandler);
          addEventListener(MouseEvent.DOUBLE_CLICK, doubleClickHandler);

          Then, within the class I added the following functions:

          //possible event listeners?
          private function clickHandler(event:MouseEvent):void{
          var tempES:String = "type : "+event.type+" objectName : "+event.currentTarget;
          trace(tempES);
          }
          private function doubleClickHandler(event:MouseEvent):void{
          var tempES:String = "type : "+event.type+" objectName : "+event.currentTarget;
          trace(tempES);
          }

          I get nothing. I suspect that I get no results because the Application has the buttons and these listeners are not referencing the application properly to listen for button clicks.

          Am I off base? Can anyone help me understand how to listen for click activity and get the object information even though the listener code is in an imported class?

          Thank you
          • 2. Re: ? Derive a clicked button's 'level' and instance  name?
            peterent Level 2
            Click events bubble, so for example:

            <mx:Application click="aButtonClick(event)" >
            <mxButton id="one" />
            <mx:Button id="two" />
            ...

            When either button one or two is clicked, the aButtonClick event handler will be invoked (notice that no button has a click handler). Same would be true if you dynamically created the buttons.