Attaching .swf assets to Flex3 by calling getDefinitionByName()
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}" />
<开发者_如何学Pythonmx:Button label="4" icon="{Symbols.HEARTS}" />
<mx:UIComponent id="spriteHolder" width="200" height="200"/>
</mx:VBox>
</mx:Application>
just go with Symbols[suit]
. object[expression]
is equivalent to object.ident
if String(expression)
evaluates to "ident"
.
精彩评论