I’m an experienced AS developer, but this is something very basic about flex that I can’t figure out. In the class below, I’m not sure why the function imageLoaded would not be executed when the image loads a url. Is there a race condition happening here? The application is completely loaded by the time this object is created and setPicture is called. How can I get the Event.COMPLETE event to fire properly in this case?
The line and Log.info functions are conveniences for me, and they definitely work.
Here’s my MXML definition:
<?xml version="1.0" encoding="utf-8"?>
<photo:PhotoClass xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:photo="com.xxx.widgets.photo.*">
<mx:Image id="image"/>
</photo:PhotoClass>
Here’s my actionscript:
public class PhotoClass extends Box {
public var image : Image;
public function PhotoClass() {
image = new Image();
image.addEventListener(Event.COMPLETE, imageLoaded);
}
private function line(txt : *) : void {
Log.info(txt, "PhotoClass");
}
public function setPicture(url : String) : void {
line("setPicture: " + url);
image.source = url;
}
public function imageLoaded(event : Event) : void {
line("image loaded");
}
}
I figured it out…
eventListeners won’t register unless the sprite is attached to the application. The eventListener in this case was being added in the constructor, before the sprite was added to its parent class. I moved image.addEventListener to setPicture and it worked.