【问题标题】:Get ids in Action script/mxml在 Action script/mxml 中获取 id
【发布时间】:2011-04-25 10:20:11
【问题描述】:

下面的代码如何获取按钮的水果图像的所有ID。是否有类似的类属性并获取ID然后......

 <?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>

【问题讨论】:

    标签: actionscript-3 flash mxml


    【解决方案1】:

    尝试使用以下内容:

    <?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>
    

    但您似乎可以使用List 或Repeater 以更优雅的方式解决您的任务。只是不知道您的要求。

    【讨论】:

      猜你喜欢
      • 2015-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-26
      • 1970-01-01
      • 1970-01-01
      • 2010-10-30
      • 1970-01-01
      相关资源
      最近更新 更多