使用包含影片剪辑符号链接标识符的字符串时如何将参数传递给构造函数

How to pass arguments to constructor when using string containing linkage identifier for a movie clip symbol

我正在使用 TextField 的 AS3 htmlText 属性,我想将自定义精灵嵌入 html。 The documentation 表示,您可以为此使用 库中影片剪辑元件的链接标识符 。所以我写道:

public class ImageWrapper extends Sprite
{
    [Embed(source = 'test.png')]
    private var TestImg:Class;

    public function ImageWrapper()
    {
        var bitmap:Bitmap = new TestImg();
        addChild(bitmap);
    }
}

然后 htmlText:

myTextField.htmlText = "Test <img src='" + getQualifiedClassName(ImageWrapper) + "'>";

这非常有效。但是当我尝试通过构造函数传递任何东西时,将定义更改为

public function ImageWrapper(foo:int)

并将 html 更新为:

"Test <img src='" + getQualifiedClassName(ImageWrapper) + "(400)'>";

我得到 Unhandled IOErrorEvent:. text=Error #2035: URL Not Found.

我搜索了文档,但没有找到为构造函数传递参数的方法。我错过了什么,或者根本不可能这样做吗?我只需要将原语发送到我的构造函数(如 intString

是的,不可能将任何附加参数传递给 html 图像 class,唯一的方法是拥有尽可能多的 classes 选项。

我相信我找到了解决方法:

public class ImageWrapper extends Sprite
{
    [Embed(source = 'test.png')]
    private var TestImg:Class;
    public static var x:int = 0;
    private var loc_x:int;

    public function ImageWrapper()
    {
        loc_x = x;
        var bitmap:Bitmap = new TestImg();
        addChild(bitmap);
    }
}

然后在我的代码中:

ImageWrapper.x = 400;
myTextField.htmlText = "Test <img src='" + getQualifiedClassName(ImageWrapper) + "'>";
ImageWrapper.x = 200;
myOtherTextField.htmlText = "Test2 <img src='" + getQualifiedClassName(ImageWrapper) + "'>";

缺点是您只能在单个 htmlText 中使用与 ImageWrapper 定义一样多的 ImageWrapper(因此您需要创建 类 ImageWrapper1ImageWrapper2, 等等)