试图访问actionscript中的图像.source信息 - 由于某种原因它没有显示出来

时间:2011-11-02 04:33:24

标签: actionscript flash-builder mxml

我正在处理一个项目(使用Flash Builder 4.5),用户可以点击其中一个图片来执行操作。这些图像都通过actionscript加载到UIComponents数组中。

我有一个私人变量如下:

private var _selectedChild:UIComponent;

跟踪哪个UIComponent当前"被选中" (是点击的最后一项)。

我只想在点击图片时显示警告,显示其ID和源文件名。

Alert.show("Current id: " + _selectedChild.id + " -- filename: " + _selectedChild.source);

使用_selectedChild.id可以很容易地输出id,但是没有.source这样的东西 - 我在整个eclipse给出的可能变量列表中查看了,我无法弄清楚哪一个会显示url或来源。我觉得我可能会遗漏一些简单的东西 - 有人知道如何从UIComponent获取这些信息吗?

这是相关的mxml:

<dp:Test id="test" width="100%" height="100%" >
        <mx:Image id="i1" source="images/i1.jpg"/>
        <mx:Image id="i2" source="images/i2.jpg"/>
    </dp:Test>

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:2)

您正在将mx:Image对象转换为基本UIComponent,它没有属性“source”。将_selectedChild保留为不明确的*,类型,或将其定义为mx.controls.Image。如果切换到spark,请使用spark.components.Image。

另外,为了在使用模糊类型时安全,您可以根据属性执行检查:

if(_selectedChild.hasOwnProperty("source"))
{
    // do stuff
}

答案 1 :(得分:1)

如果我正确理解您的代码,您需要首先将UIComponent转换为Image:

var image:Image = _selectedChild as Image;
if (!image) trace("Nothing selected or the child is not an image");
Alert.show("Current id: " + image.id + " -- filename: " + image.source);
相关问题