我有一个测试用例来证明我的问题。
当您在 Flash Builder 4.6 中将以下 4 个短文件添加到新的 Flex Mobile 项目中时,它会立即运行:
源/TestFXG.mxml:
<?xml version="1.0" encoding="utf-8"?>
<s:ViewNavigatorApplication
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
firstView="Home">
</s:ViewNavigatorApplication>
src/assets/en/Star.fxg:
<?xml version='1.0' encoding='UTF-8'?>
<fxg:Graphic xmlns:fxg="http://ns.adobe.com/fxg/2008" version="2">
<fxg:Path x="9.399" y="10.049" data="M 82.016 78.257 L 51.895 69.533 L 27.617 89.351 L 26.621 58.058 L 0.231 41.132 L 29.749 30.52 L 37.714 0.241 L 56.944 24.978 L 88.261 23.181 L 70.631 49.083 Z">
<fxg:fill>
<fxg:SolidColor color="#FFFFFF"/>
</fxg:fill>
<fxg:stroke>
<fxg:SolidColorStroke
caps="none"
color="#FFFF66"
joints="miter"
miterLimit="4"
weight="10"/>
</fxg:stroke>
</fxg:Path>
</fxg:Graphic>
src/Home.mxml:
<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:my_components="*"
title="Display random amount of stars">
<fx:Script>
<![CDATA[
import Star;
import spark.core.SpriteVisualElement;
private static const STAR:SpriteVisualElement = new Star();
]]>
</fx:Script>
<my_components:MyComp />
</s:View>
src/MyComp.as:
package {
import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.utils.getDefinitionByName;
import mx.core.UIComponent;
import spark.core.SpriteVisualElement;
import assets.Star;
public class MyComp extends UIComponent {
private static const WHAT:String = "assets.en.Star";
override protected function createChildren():void {
super.createChildren();
for (var i:uint = 0; i < 3; i++) {
var star:Star = new Star();
//var star:SpriteVisualElement = new (getDefinitionByName(WHAT) as Class)();
star.x = Math.random() * 100;
star.y = Math.random() * 100;
addChild(star);
}
}
}
}
我的问题是:如果我在 assets 子目录中有许多适当命名的 FXG 文件(在我的实际应用程序中,我有 spades_queen.fxg、spades_king.fxg、spades_ace.fxg 等) - 我如何在运行时选择 FXG 文件?
上面的注释行给了我运行时错误:
ReferenceError: Error #1065: Variable Star is not defined.
at global/flash.utils::getDefinitionByName()
当我将src/assets/en/Star.fxg 移动到src/Star.fxg 时,一切正常......