通过调用 getDefinitionByName() 将 .swf 资源附加到 Flex3
如何将 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需使用
Symbols[suit]
即可。如果String(expression)
计算结果为"ident"
,则object[expression]
相当于object.ident
。just go with
Symbols[suit]
.object[expression]
is equivalent toobject.ident
ifString(expression)
evaluates to"ident"
.