Well man, if your question is why the bootloader is alive out of scope, here is your answer:
In fact, the loader that you see in the class constructor (CCM) method is not the one you see in onComplete: LINE14: var loader:URLLoader = URLLoader(evt.target);
(I don’t know why people like to enter the same name into variables, which can be confusing, but now it’s not, if I explain)
The magic lies in evt.target . But you must ask yourself: "What does .target do?" Well, this is an instance variable defined by "Event Object", and contains a link to "Target".
If you do not know what the “Target” is, read this paragraph. "Target" is an object in which the listener is registered in LINE9: loader.addEventListener(Event.COMPLETE, onComplete); As you can see, this is the bootloader in CCM (please do not confuse the bootloader names), which refers to new URLLoader(); . Section Conclusion: “Target” is a URLLoader object referenced by the local load variable in the CCM.
Well, using the .target variable, you can have a reference to a URLLoader object, and then use the link as you want. In this case, you used the link to remove the listener. Everything is good.
Here is an improved version (only one instance variable that gives a link to URLLoader):
package { import flash.display.Sprite; import flash.events.*; import flash.net.*; public class URLLoaderExample extends Sprite { private var lalala:URLLoader; public function URLLoaderExample() { lalala = new URLLoader(); lalala.addEventListener(Event.COMPLETE, onComplete); lalala.load(new URLRequest("example.txt"); } private function onComplete(evt:Event):void { trace ("Received data: " + lalala.data); lalala.removeEventListener(Event.COMPLETE, onComplete); } } }
But just to make shure, you will not be confused with the names:
package { import flash.display.Sprite; import flash.events.*; import flash.net.*; public class URLLoaderExample extends Sprite { public function URLLoaderExample() { var blabla:URLLoader = new URLLoader(); blabla.addEventListener(Event.COMPLETE, onComplete); blabla.load(new URLRequest("example.txt"); }
Greetings ... If you have any doubts about the casting operation used in URLLoader(evt.target); , you can ask.