|
-
Total Universe Mod
[AS2] Class instances overwriting each other
I'm essentially re-packaging the MovieClipLoader class using composition. Each instance requires a reference to a listener object. When I create two instances of the class and reference a separate listener object for each, the second one occurring in script is being used by both instances. Rearranging the code proves that it's the last object occurring in my script that is the only one being used.
I may not be forwarding the event functions correctly but it works just fine when using only one instance of my preloader class.
Here is a snippet from the class:
Code:
public function get onLoadProgress():Function{
return mcl_L.onLoadProgress;
}
public function set onLoadProgress(func:Function){
mcl_L.onLoadProgress = func;
}
and the usage:
Code:
myPreloaderInstance.onLoadProgress = function(target, bytesLoaded, bytesTotal){
//update code
}
Like I said it works just fine for one instance. Would using inheritance solve this?
-
Bearded (M|G)od
Are you just trying to get rid of the need to attach a listener? If so, I wrote this a long time ago:
Code:
/*
* ClipLoader.as
*
* All code/functions/classes
* written by: Matt Robenolt
* Copyright © 2007 YDEK Productions LLC
* http://www.ydekproductions.com
* [email protected]
*
*/
/**
* A wrapper for the MovieClipLoader class
* which adds the listener automatically.
*/
class com.ydekproductions.loading.ClipLoader extends MovieClipLoader
{
public var onLoadComplete:Function; //function(targetMc:MovieClip, httpStatus:Number)
public var onLoadError:Function; //function(targetMc:MovieClip, errorCode:String, httpStatus:Number)
public var onLoadInit:Function; //function(targetMc:MovieClip)
public var onLoadProgress:Function; //function(targetMc:MovieClip, loadedBytes:Number, totalBytes)
public var onLoadStart:Function; //function(targetMc:MovieClip)
/**
* Constructor
*/
public function ClipLoader()
{
super();
addListener(this);
}
}
It just wraps the MovieClipLoader, and allows you to use the ClipLoader.onLoadProgress, onLoadInit, etc.
-
Total Universe Mod
Thanks man. I have a similar class I wrote a while back too but I'm trying to get away from the bad as1.0 habit of arbitrarily creating object properties. Looks like I'll have to go that route anyway. The class I'm writing now also includes directives for use with UI.
Either way, it doesn't make sense that somehow method assignments on two different instances would be overriding each other in some 'global' place.
Last edited by jAQUAN; 03-03-2008 at 09:28 PM.
-
Bearded (M|G)od
Post the whole class and I'll take a look. It shouldn't be doing that either. Are you defining things as static attributes?
-
Total Universe Mod
Code:
class PreLoader{
private var mcl_L:Object = {};
private var loadStart:Function;
private var loadInit:Function;
private var loadProgress:Function;
private var loadComplete:Function;
private var mcl:MovieClipLoader;
public function PreLoader(){
mcl = new MovieClipLoader();
mcl.addListener(mcl_L);
}
public function loadClip(url:String, target:Object){
trace("pre loadClip");
mcl.loadClip(url, target);
}
public function get onLoadStart():Function{
return mcl_L.onLoadStart;
}
public function set onLoadStart(func:Function){
mcl_L.onLoadStart = func;
mcl.addListener(mcl_L);
}
}
I'm gonna try the subclass approach again too.
-
Total Universe Mod
Hmm, subclassing works all of a sudden. Chances are that I had something private that shoulda been public. I must have fixed something while debugging the composition version. :{
-
Bearded (M|G)od
Haha, yeah, for doing that, you might as well inherit from MovieClipLoader. That's really exactly what you are doing. Just do exactly what I did, and you won't have any problems. It'll work exactly the way MovieClipLoader does, except without the listener and it has callback functions.
-
Total Universe Mod
Yeah that's what I ended up doing. I really just want make preloaders a one step process but this package is for a 3rd party so the look and function of the graphics had to be easily modified.
I'm thinking about requiring a start, update and complete (and/or init) functions in the constructor as in,
loader1 = new PreLoader(updateFunc, startFunc, completeFunc);
What do you think?
-
Bearded (M|G)od
If you want to do that, you can easily modify my script for it. Do this:
Code:
public function ClipLoader(updateFunc:Function, startFunc:Function, completeFunc:Function)
{
super();
onLoadProgress = updateFunc;
onLoadStart = startFunc;
onLoadComplete = completeFunc;
addListener(this);
}
-
Total Universe Mod
Thanks but I'm clear on how to do it. I'm just wondering how you think the end user would appreciate that. Would you rather create a PreLoader object, then set it's event functions, or would you rather write some functions then pass them to the PreLoader constructor? I know looking up functions is slower than direct references but it's not like this will be used for games.
At any rate, thanks for the input.
-
Bearded (M|G)od
It all depends on your end user. Are you planning on releasing something for the community to use? If so, you're probably going to want to keep things as open ended as possible. Give them as many options so it can suit their needs.
If it's being made for a specific project, you may just want to use the functions inside the constructor to keep things cleaner and have a 1 line declaration instead of 4.
So it's really up to who exactly the end user of this class will be.
Also, in either case, performance should be exactly the same. You're doing the exact same thing either way, just a different way of writing it.
-
Total Universe Mod
It's back!
Everything worked out fine for what I was trying to do. But now I'm cramming in another feature that requires the preloader to conditionally call the functions assinged to its events without the user having to supply the if() statement.
I'm having a terrible time with a scope issue. for some reason, loadStart() cannot find a function within its very same class when called from an object listener.
//in class PreLoader.as
Code:
class PreLoader extends MovieClipLoader{
private var mcl_L:Object = {};
public var catchLoadStart:Function;
public function PreLoader(){
mcl_L.onLoadStart = loadStart;
addListener(mcl_L);
}
public function set onLoadStart(func:Function){
this.catchLoadStart = func;
}
public function loadStart(target){
//this will contain an if statement once I get this working
trace('internal loadStart'); //this line works
this.catchLoadStart(target); //this line doesnt! wth!?
}
}
any thoughts?
-
Total Universe Mod
Got it!
I just had catch the user submitted function onto a property of mcl_L and then bypass the compiler when accessing it by evaluating it. Ugg!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|