How do I refer to a dynamically created object in AS3? (Added Moviclip to scene)
This is what has annoyed me since I was doing AS3 a year ago.
Example. I'm making a class that extends the Movie Clip and calls it " LoaderBar
", it's all text that says "Loading", and below that another clip, which is a simple rectangle that I call " lBar
"
When I call the function to load the image, I add the Loader to the scene as such.
function imageLoader(URL:String):void
{
var loader:Loader = new Loader(new URLRequest(URL));
loader.contentLoaderInfo.addEventListner(ProgressEvent.PROGRESS, progressHandler);
var loadBar:Loader Bar = new LoaderBar();
addChild(loadBar);
}
function progressHandler(e:Event):void
{
var pcent:Number = e.getBytesLoaded / e.getBytesTotal;
// HERE IS WHERE I'D LIKE TO MAKE DIRECT REFERENCE TO MY LOADBAR;
loadBar.lBar.width = pcent*100;
}
Basically, I just want to tell lBar
the MovieBar LoadBar the percentage width * 100. (so when the clip is loaded, the loader bar is 100px wide).
This is my problem. When I add a loadBar to the scene inside a function, I cannot refer to it inside another function without doing some kind of hack making a global variable outside of my function, like ...
var loadBarClip:MovieClip;
and inside the load function, assigning loadBar
loadBarclip
as such
loadBarClip = loadBar.
I feel like this is redundant. Does anyone know about accessing mine loadBar
without creating a reference variable?
source to share
If it's just for that handler, you can make the handler anonymous and go into the current scope.
var loadBar = new LoaderBar();
var loader:Loader = new Loader(new URLRequest(URL));
loader.contentLoaderInfo.addEventListner(
ProgressEvent.PROGRESS, function (e:Event):void {
var pcent:Number = e.getBytesLoaded / e.getBytesTotal;
loadBar.lBar.width = pcent*100; //Here you are making a direct reference.
}
);
If you really want to encapsulate your scopes, you can use closure:
returnFromEncapulatingClosure = function(){
var loadBar = new LoaderBar();
var loader:Loader = new Loader(new URLRequest(URL));
return {
loadBar: loadBar,
loader: loader
};
}();
This allows you to concatenate some links so that they don't confuse other parts of the code, and you could link to it with
returnFromEncapulatingClosure.loader.contentLoaderInfo.addEventListner(ProgressEvent.PROGRESS, progressHandler);
function progressHandler(e:Event):void {
var pcent:Number = e.getBytesLoaded / e.getBytesTotal;
returnFromEncapulatingClosure.loadBar.lBar.width = pcent*100;
}
As a footnote, when you extend a Movie Clip, add a method that sets lBar.width. Something like:
loadbar.setLBarWidth = function (w:number) {
this.lBar.width = w;
}
source to share
I don't see a major problem with a variable declared outside of the imageLoader. If you write this in a class instead of a timeline, it will just be a class variable and there is nothing wrong with that. They exist for this very reason.
If your deadset wants to store the loadBar variable locally, you can always do this:
in the imageLoader function:
var loadBar:Loader Bar = new LoaderBar();
loadBar.name = "loadBar";
addChild(loadBar);
in progressHandler function:
getChildByName("loadBar");
source to share