0

I'm working on a project (using flash builder 4.5) where a user can click on one of a number of pictures to perform an action. The images are all loaded into an array of UIComponents through actionscript.

I have a private variable as follows:

private var _selectedChild:UIComponent;

Which keeps track of which UIComponent is currently "selected" (was the last item clicked).

I just want to show an Alert when the picture is clicked displaying it's id and the source filename.

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

The id comes out easy with _selectedChild.id, but there is no such thing as .source - I looked throughout the entire list of possible variables eclipse gives me and I can't figure out which one would display the url or the source. I feel like I might be missing something easy - does anyone know how to get this information from a UIComponent?

This is the relevant 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>

Any help is much appreciated.

Anthony
  • 2,411
  • 6
  • 26
  • 23

2 Answers2

2

You are casting an mx:Image object to the base UIComponent, which does not have property "source". Either leave _selectedChild as an ambiguous, *, type, or define it as mx.controls.Image. If you switch to spark, use spark.components.Image.

Alternativly, to be safe while using an ambiguous type, you can perform a check based for the property:

if(_selectedChild.hasOwnProperty("source"))
{
    // do stuff
}
stat
  • 411
  • 2
  • 6
1

If I understand your code correctly, you need to cast the UIComponent to an Image first:

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);
laurent
  • 88,262
  • 77
  • 290
  • 428