Get ids in Action script/mxml

≡放荡痞女 提交于 2019-12-13 04:06:17

问题


Io the following code how to get all the ids for fruits image onlcick of the button.Is there like a class attribute and get the ids then....

 <?xml version="1.0" encoding="utf-8"?>
  <mx:Application layout="absolute" xmlns:mx="http://www.adobe.com/2006/mxml">
      <mx:Script>
      <![CDATA[
          import mx.effects.easing.Quadratic;
          public function clickhandler(event:Event):void
          {
              //How to get all the ids of fruits.jpg image only
          }
      ]]>
      </mx:Script>

      <mx:Move id="fruitAnimation1" target="{fruitImage}" xTo="100" yTo="10" />

      <mx:Canvas backgroundColor="#A9C0E7" borderStyle="solid" height="800" id="myCanvas" width="800">
          <mx:Image  height="50" id="fruitImage" source="@Embed(source='fruits.jpg')" width="50" x="100" y="10" />
          <mx:Image  height="50" id="fruitImage1" source="@Embed(source='fruits.jpg')" width="50" x="150" y="10" />
          <mx:Image  height="50" id="fruitImage2" source="@Embed(source='fruits.jpg')" width="50" x="200" y="10" />
          <mx:Image  height="50" id="fruitImage3" source="@Embed(source='fruits.jpg')" width="50" x="250" y="10" />

          <mx:Image height="200" source="@Embed(source='box.jpg')" width="200" x="300" y="350" />
      </mx:Canvas>
  <mx:Button label="Click" click="clickhandler(event)"  x="100" y="316"/>
  </mx:Application>

回答1:


Try to use the following:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application layout="absolute" xmlns:mx="http://www.adobe.com/2006/mxml">
    <mx:Script>
    <![CDATA[
        [Bindable("__NoChangeEvent__")]
        [Embed(source="fruits.jpg")]
        private var fruitImageClass:Class;

        public function clickhandler(event:Event):void
        {
            var numChildren:int = myCanvas.numChildren;
            for (var i:int = 0; i < numChildren; i++)
            {
                var child:DisplayObject = myCanvas.getChildAt(i);
                if (child is Image)
                {
                    var image:Image = Image(child);
                    if (image.source == fruitImageClass)
                        trace(image.id);
                }
            }
        }
    ]]>
    </mx:Script>

    <mx:Move id="fruitAnimation1" target="{fruitImage}" xTo="100" yTo="10" />

    <mx:Canvas backgroundColor="#A9C0E7" borderStyle="solid" height="800" id="myCanvas" width="800">
        <mx:Image height="50" id="fruitImage" source="{fruitImageClass}" width="50" x="100" y="10" />
        <mx:Image height="50" id="fruitImage1" source="{fruitImageClass}" width="50" x="150" y="10" />
        <mx:Image height="50" id="fruitImage2" source="{fruitImageClass}" width="50" x="200" y="10" />
        <mx:Image height="50" id="fruitImage3" source="{fruitImageClass}" width="50" x="250" y="10" />

        <mx:Image height="200" source="@Embed(source='box.jpg')" width="200" x="300" y="350" />
    </mx:Canvas>
    <mx:Button click="clickhandler(event)" label="Clidk" x="100" y="316" />
</mx:Application>

But it seems to be you can solve your task more elegant way using List or Repeater. Just have no idea about your requirements.



来源:https://stackoverflow.com/questions/5777411/get-ids-in-action-script-mxml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!