通过调用getDefinitionByName()将.swf资产附加到Flex3

时间:2010-03-25 08:26:20

标签: flex actionscript-3 flex3

如何在Flex3文件的Actionscript部分中附加.swf文件中的符号?

我准备了一个简单的测试案例来证明我的问题。一切正常(4个按钮上有图标,有一个红色圆圈) - 除了getDefinitionByName()部分。

我的目标是“动态地”附加库中的符号 - 即取决于运行时套装变量的值。

Symbols.as:

package {
    public class Symbols {
        [Embed('../assets/symbols.swf', symbol='spades')]
        public static const SPADES:Class;

        [Embed('../assets/symbols.swf', symbol='clubs')]
        public static const CLUBS:Class;

        [Embed('../assets/symbols.swf', symbol='diamonds')]
        public static const DIAMONDS:Class;

        [Embed('../assets/symbols.swf', symbol='hearts')]
        public static const HEARTS:Class;
    }
}

TestCase.mxml:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
    layout="absolute"
    creationComplete="onCreationComplete();">
    <mx:Script>
       <![CDATA[
           private function onCreationComplete():void
           {
                var sprite:Sprite = new Sprite();
                var g:Graphics = sprite.graphics;

                g.lineStyle(1, 0xFF0000);
                g.beginFill(0xFF0000);
                g.drawCircle(100, 100, 20);
                g.endFill();

                   spriteHolder.addChild(sprite);

                // XXX stuff below not working, can it be fixed?
                   var suit:String = "SPADES";
                   var mc:MovieClip = new (getDefinitionByName("Symbols.SPADES") as Class);
                   spriteHolder.addChild(mc);
           }
       ]]>
    </mx:Script>
    <mx:VBox width="100%">       
        <mx:Button label="1" icon="{Symbols.SPADES}" />
        <mx:Button label="2" icon="{Symbols.CLUBS}" />
        <mx:Button label="3" icon="{Symbols.DIAMONDS}" />
        <mx:Button label="4" icon="{Symbols.HEARTS}" />
         <mx:UIComponent id="spriteHolder" width="200" height="200"/>       
    </mx:VBox>   
</mx:Application>

1 个答案:

答案 0 :(得分:1)

Symbols[suit]一起使用。如果object[expression]评估为object.ident,则String(expression)相当于"ident"