动态选择静态嵌入图像?

时间:2012-02-25 18:57:33

标签: actionscript-3 flex air adobe flash-builder

这是我目前的情况:

我在课堂上嵌入了图片。

package system
{
    public class Embedded
    {
        [Embed(source="assets/srcorangeboxidle.png")]
        public static const btnSrcOrangeBoxIdle:Class;

        [Embed(source="assets/srcorangeboxpressed.png")]
        public static const btnSrcOrangeBoxPressed:Class;

        [Embed(source="assets/hl1idle.png")]
        public static const btnHL1Idle:Class;

        [Embed(source="assets/hl1pressed.png")]
        public static const btnHL1Pressed:Class;

        public function Embedded(){}
    }
}

现在在我的主MXML文件中,我有以下内容。

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

    <fx:Script>
        <![CDATA[
            import system.Embedded;

            protected function toggleButtonState(target:Object,pressed:Boolean=false):void
            {
                var baseImageName:String = target.id.toString();
                if (!pressed) {
                    target.source = Embedded.[baseImageName+"Idle"];
                } else {
                    target.source = Embedded.[baseImageName+"Pressed"];
                }
            }
        ]]>
    </fx:Script>
    <s:Image id="btnSrcOrangeBox" x="107" y="245" source="{Embedded.btnSrcOrangeBoxIdle}" mouseDown="toggleButtonState(btnSrcOrangeBox,true)" mouseUp="toggleButtonState(btnSrcOrangeBox,false)"/>
    <s:Image id="btnHL1" x="107" y="355" source="{Embedded.btnHL1Idle}" mouseDown="toggleButtonState(btnHL1,true)" mouseUp="toggleButtonState(btnHL1,false)"/>
</s:Application>

正如您所知,上述代码似乎没有成功。我只想动态选择要选择的 Embedded。* 类。任何帮助或提示将不胜感激。

1 个答案:

答案 0 :(得分:3)

删除班级名后的点。以下应该有效:

target.source = Embedded[baseImageName + "Idle"];