简体   繁体   中英

Action script3 trace in flex builder hierarchy output

i am using Flex builder 4.5 and my problem is this that this code doos not work, when i used trace(event.target) i get following in result in console,

deleteme.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.VGroup5.button1

And if i replace this long line in 'if' statement code works.(deleteme is the project name). Dont you think it should only say button1 instead of this all long line with all hierarchy , if that is the case then how we can shortend it?

<?xml version="1.0" encoding="utf-8"?>
 <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
           xmlns:s="library://ns.adobe.com/flex/spark" 
           xmlns:mx="library://ns.adobe.com/flex/mx" 
           minWidth="955" minHeight="600"
           initialize="handleClick(event)">
<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>

<fx:Script>
    <![CDATA[
        private function init():void{
            button2.addEventListener(MouseEvent.CLICK, handleClick);
        }
        private function handleClick(event:Event):void{
            trace(event.target);
            if(event.target == "button1"){
                button1.label = "Button 1 clicked";
            }else if(event.target == "button2"){
                button2.label = "Button 2 clicked";
            }
        }

    ]]>
</fx:Script>
<s:VGroup width="100%">
    <s:Button id="button1" label="Button 1" click="handleClick(event)"/>
    <s:Button id="button2" label="Button 2" />
</s:VGroup>
</s:Application>

thanks in advance,

(i tried with sdk 4.1 still same answer)

Use the following code instead:

<?xml version="1.0" encoding="utf-8"?>
 <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
           xmlns:s="library://ns.adobe.com/flex/spark" 
           xmlns:mx="library://ns.adobe.com/flex/mx" 
           minWidth="955" minHeight="600"
           initialize="handleClick(event)">
<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>

<fx:Script>
    <![CDATA[
        private function init():void{
            button2.addEventListener(MouseEvent.CLICK, handleClick);
        }
        private function handleClick(event:Event):void{
            trace(event.target);
            if(event.currentTarget == button1){
                button1.label = "Button 1 clicked";
            }else if(event.currentTarget == button2){
                button2.label = "Button 2 clicked";
            }
        }

    ]]>
</fx:Script>
<s:VGroup width="100%">
    <s:Button id="button1" label="Button 1" click="handleClick(event)"/>
    <s:Button id="button2" label="Button 2" />
</s:VGroup>
</s:Application>

It has no sense to compare visual objects with strings. Compare objects with objects themselves.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM