Attaching .swf assets to Flex3 by calling getDefinitionByName()

Posted by Alexander Farber on Stack Overflow See other posts from Stack Overflow or by Alexander Farber
Published on 2010-03-25T08:26:20Z Indexed on 2010/03/25 8:33 UTC
Read the original article Hit count: 536

Hello!

Does anybody please know, how could you attach symbols from an .swf file in the Actionscript part of your 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.

Thank you, Alex

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>

© Stack Overflow or respective owner

Related posts about flex

Related posts about actionscript-3