Hello,
I wish to use a button to load an external image. However it does not work. The codes are:
import flash.display.Sprite;
import flash.events.Event;
var logo:Sprite = new Sprite();
var myLoader = new Loader();
myLoader.load(new URLRequest(“myimage.png”));
function checkComplete(evt:MouseEvent):void {
myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadImage);
}
loadPicture_btn.addEventListener(MouseEvent.CLICK, checkComplete);
function loadImage(evt:Event):void {
logo.addChild(myLoader);
addChild(logo);
}
It seems that the loadImage function cannot be called from within the checkComplete function. How can I fix the problem?
Thanks and best regards
Alex
First you need to import URLrequest and Loader classes. You should get some compiler errors if you’re not loading those…
Maybe I’m simply not getting what you’re trying to do… but if you want to load the external picture when you press the button… then why are you adding a listener to check if the load is complete instead of simply loading the picture when you press the button?
loadPicture_btn.addEventListener(MouseEvent.CLICK, loadPic);
function loadPic():void{
myLoader.load(new URLRequest(“myimage.png”));
}
myLoader.contentLoaderInfo.addEventListener(Event. COMPLETE, loadComplete);
function loadComplete(event:Event):void{
trace(“pic loaded!”);
//do whatever like addChild(event.target.content);
}
Doesn’t that make more sense?
Hello,
Thanks for your help. It works. However I am quite confuse. Here’s your code again:
loadPicture_btn.addEventListener(MouseEvent.CLICK, loadPic);
function loadPic():void{
myLoader.load(new URLRequest(“myimage.png”));
}
myLoader.contentLoaderInfo.addEventListener(Event. COMPLETE, loadComplete);
function loadComplete(event:Event):void{
trace(“pic loaded!”);
//do whatever like addChild(event.target.content);
}
I always think that the “message path” need to follow one by one. That is, click a Button, then run a function, then the function call another function, etc… until the end of all the codes.
In your example, I always think that:
- a user click the loadPicture_btn, then
- the function loadPic will be called and run.
- the “message path” will be end here since the function loadPic do not call another function to run.
And I always think that. When the movie start, only the following line will be run:
myLoader.contentLoaderInfo.addEventListener(Event. COMPLETE, loadComplete);
Is it true that (Please correct me if I am wrong):
-
when a user click the loadPicture_btn, then
-
the function loadPic will be called and run, and
-
myLoader.load(new URLRequest(“myimage.png”)); will “initiate”
myLoader.contentLoaderInfo.addEventListener(Event. COMPLETE, loadComplete);
-
the function loadComplete will then be called when the loading is complete
Thanks and best regards
Alex
Hey Alex
I’m not sure if I’m understanding what you’re saying…
A listener is something that listens for something to happen 
When you write my_object.addEventListener(Event.COMPLETE,my_function)
You’re saying: “create this listener and listen to my_object for the COMPLETE event. When that happens launch my_function”.
Or said in more technical way: the listener object waits until a certain event is fired. When the event is fired (such as Event.COMPLETE) then my_function is called, and an event object is passed to the function. This is why you declare my_function with an event object as a parameter:
function my_function(my_event_object:Event):void{
//bla bla bla
}
The event object as a parameter is obligatory in the function that is called by a listener.
Then you can do all sorts of things with the object that is sent to the function like:
my_event_object.target //to know which object fired the event
or if you’re loading an external picture you can do something like:
addChild(my_event_object.target.content) //which means “add the the loaded content to that display object”
Is that what you want to know?
Hope that helps 
Hi Pier,
Thanks for your information. Very helpful, clear and easy to understand. I really learn a lot.
Best regards
Alex