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

发布于 2024-08-26 10:41:43 字数 2076 浏览 3 评论 0原文

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

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

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

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>

How to attach symbols from an .swf file in the Actionscript part of the Flex3 file?

I've prepared a simple test case demonstrating my problem. Everything works (there are icons at the 4 buttons, there is a red circle) - except the getDefinitionByName() part.

My target is to attach a symbol from library "dynamically" - i.e. depending at the value of the suit variable at the runtime.

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情域 2024-09-02 10:41:43

只需使用Symbols[suit]即可。如果 String(expression) 计算结果为 "ident",则 object[expression] 相当于 object.ident

just go with Symbols[suit]. object[expression] is equivalent to object.ident if String(expression) evaluates to "ident".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文