Why do I have to use global ref to 'this' in my proto?

Hi guys and girls,

Ive come across this before but i cant remeber for the life of me the solution :(. For some reason I cannot call methods in my prototype more than once via this.myMethod()…I have to store a reference to ‘this’ in the constructor using _global.ref=this; and then call my functions via ref.myFunction()…it’s real dumb. Although my proto sorta works…

Thanks in advance :love:

say wha? :huh:

Ok, sorry I should have simplified my explanation a bit. :stuck_out_tongue:

Essentially Sen Im trying to create a global Object which carries out repitive movie actions for my site.

The main function is loadContent which is used to load the various sections in my site. Probably the most complicated the typical scenario for this function is when a user clicks a menu item. It fades current movie to 0 alpha (using the handy $tweenManager class) unloads it (via unloadMovie), starts preloading the next, and finally on load complete fades up the newly loaded movie…

It works, well sort of… it seems since I only have 1 Object that if a user suddenly decides to press another menu item mid-load my function loadContent complely ignores it UNTIL it has completely finished loading previosuly selected movie…

**** I really need to post the code…its hard to put in words. Could you possibly skim through at least Sne and point me in the right direction…my architecture is poor as you can see :(.


#include "as_functions/string_utils.as"
#include "as_functions/movieclip_utils.as"
#include "as_functions/tween_utils.as"

movieActions = function (scope, container) {
	this.container = container;
	trace("this.container._alpha: "+this.container._alpha);
	this.scope=scope;
	_global.ref=this;
	this.init();
};
movieActions.prototype = new Object();
movieActions.prototype.init = function() {
	trace("init called");
	trace("the scope is: "+this.scope);
	this.boxLoader = this.scope.createEmptyMovieClip("boxLoader_mc", 1);
	this.boxLoader._alpha=0;
	//required width and height
	this.boxWidth = this.scope._width;
	this.boxHeight = this.scope._height;
	// total line length
	this.boxLength = (this.boxWidth*2) + (this.boxHeight*2);
	
};
movieActions.prototype.loadContent = function(path, delay, cache, postArray) {
	if(path!=undefined && path.indexOf(".swf") != -1) {
		trace("correct arguments");
		if(typeof path !="string"){
			trace("patnot string so converting");
			var path=path.toString();//reassign path	
		}
		path = path.trim();//remove excess
		if(typeof cache !="string"){
			if(cache==false){
				path += "?id="+Math.round(Math.random()*10000);
			}
			
		}else{
			if(cache.trim() == "false" || cache.trim() == "0"){
				trace("no cache teeteced");
				path += "?id="+Math.round(Math.random()*10000);
			}
		}
		var delay=0;
		if(delay!=undefined){
			
			switch(typeof (delay)){
				
				case "string":
				//must be a string, convert to number and assign
				delay=parseInt(delay.trim());
				break;
				
				case "number":
				//must be a number so assign delay
				trace("delay of "+delay+" secs specified");
				delay=delay;
				break;
				
				default:
				//dont know what type it is so go to default
				delay=0;
				break;
			}
		}
		//Append any POST vars
		if (postArray instanceof Array && postArray.length>0) {
			trace("appending POST vars");
			var tmpArray = new Array(postArray.length);
			for (var i = 0; i<postArray.length; i++) {
				if (postArray* != undefined) {
					tmpArray* = "var"+(i)+"="+escape(postArray*.trim());
				}
				//url encode value
				}
			var queryString = tmpArray.join("&");
			// add '&' to seperate name=value pairs
			delete postArray;
			delete tmpArray;
			path+= (path.indexOf("?") == -1) ? "?"+queryString : "&"+queryString;
				
		}
		ref.updateContent(path, delay);
	} else {
			trace("Error loadContent: no file to load");
	}
};

movieActions.prototype.loadNext = function(path) {
	trace("loadNext: "+path+", "+this.container);
	trace("loader "+this.loader);
	//remove current
	unloadMovie(ref.container);
	//show preloader
	ref.boxLoader.alphaTo(100, 0, "linear", 0, undefined);
	//this.loader.label_txt._visible=true;
	//begin loading
	loadMovie(path, ref.container);
	//show preloading status
	
	ref.loadComplete = setInterval(ref, "showLoading", 40);
	
};
movieActions.prototype.updateContent = function(path, delay) {
	trace("updateContent "+this.container);
	//destAlpha, seconds, animType, delay, callback, extra1, extra2
	//fade out current, and then wait for given delay before load next
	this.container.alphaTo(0, 1, "linear", delay, {scope:this, func:this.loadNext, args:[path]});
};
movieActions.prototype.showLoading = function() {
	
	trace("showLoading "+this.container+" "+this.boxLoader);
	 this.boxLoader.clear();
        this.boxLoader.lineStyle(1, 0xDCE292, 100);
        if (this.container.getBytesTotal() > 100){
                // percent of movie loaded
                pct = Math.round((100/this.container.getBytesTotal()) * this.container.getBytesLoaded());
        } else {
                pct = 0;
        }
        
        // find the length of the line to draw from this.pct loaded
        lineSize = (this.boxLength*pct)/100;
        
        this.boxLoader.moveTo(0, 0);
        
        // line one
        if (lineSize<this.boxWidth) {
                this.boxLoader.lineTo(lineSize, 0);
        } else {
                this.boxLoader.lineTo(this.boxWidth, 0);
                // line two
                if (lineSize<this.boxWidth+this.boxHeight) {
                        this.boxLoader.lineTo(this.boxWidth, lineSize-this.boxWidth);
                } else {
                        this.boxLoader.lineTo(this.boxWidth, this.boxHeight);
                        // line three
                        if (lineSize<(this.boxWidth*2)+this.boxHeight) {
                                this.boxLoader.lineTo(((this.boxWidth*2)+this.boxHeight)-lineSize, this.boxHeight);
                        } else {
                                this.boxLoader.lineTo(0, this.boxHeight);
                                // line four
                                if (lineSize<this.boxLength) {
                                        this.boxLoader.lineTo(0, this.boxLength-lineSize);
                                } else {
                                        this.boxLoader.lineTo(0, 0);
                                }
                        }
                }
        }
        if (pct == 100) {
            trace("loadCompleted");
			//delete this.onEnterFrame;
			
			
			this.boxLoader.alphaTo(0, 1, "linear", 0, undefined);
			this.container.alphaTo(100, 1, "linear", 1, undefined);
			clearInterval(this.loadComplete);
			
			
        }
	/////////////
};
movieActions.prototype.saveSO = function(so_name){
	//mySO = SharedObject.getLocal(so_name);
	//check user allows minimum 10k to be stored
	//NB if current data is <10k it will be stored immediately and skip ref condition
	success = so_name.flush(10000);
	if (success == "pending") {
		so_name.onStatus = function(result) {
			if (result.code == "SharedObject.Flush.Success") {
				trace("Sucess writing to disk");
				writeSuccess = true;
			} else {
				trace("Failure writing to disk");
				writeSuccess = false;
			}
		};
	} else {
		writeSuccess = success;
	}
	return writeSuccess;//return result
};
movieActions.prototype.deleteSO = function(so_name){
	
	for (a in so_name.data) {
		delete so_name.data[a];
	}
	so_name.flush();
	// Delete the SO
	delete so_name;
};


movieActions.prototype.goBackOne = function() {
	if (user_so.data.locations != undefined) {
		trace("previous location: "+user_so.data.location);
	}
	//check user_so.data.location do a substring to see if prev location within same movie, then check frame number
};
movieActions.prototype.message = function(msg) {
	trace(ref+" message: "+msg);
};


main = function(){
	trace("loading main");
	//destAlpha, seconds, animType, delay, callback, extra1, extra2
	
	intro_mc.alphaTo(0, 0, "linear", 0, null);
	logo_mc.alphaTo(100,1,"easeInBack",1,null);
	menu_mc.alphaTo(100, 1, "linear", 2, null);
	clock_mc.alphaTo(100, 1, "linear", 3, null);
	Object.Actions.checkUser();//see if sharedobject
	Object.Actions.loadContent("about_inex.swf", 3, "true");
    content_mc.alphaTo(100, 1, "linear", 0, null);
};
logout_btn.onRelease = function(){
	Object.Actions.logout(user_so);
};
intro_mc.introSkip_btn.onRelease=function(){
	trace("skipping intro..");
	delete intro_mc.onEnterFrame;
	this.enabled=false;//so skip cant be pressed again
	main();
};
intro_mc.onEnterFrame=function(){
	if(this._currentframe==this._totalframes){
		//intro has finished so rewind
		this.onEnterFrame=function(){
			if(this._currentframe==1){
				//intro has been totally rewound, so load menu
				delete this.onEnterFrame;
				main();//start MAIN
			}else{
				//intro still has frames to replay
				this.prevFrame();
			}
		};
	}else{
		//intro not finished play next frame
		this.nextFrame();
	}
};
Object.Actions = new movieActions(content_mc, content_mc.target_mc);
logo_mc._alpha=menu_mc._alpha=clock_mc._alpha=0;


stop();

Hello? Anybody please assist

Thanks in advance

Hmm well, ive narrowed it all down to 1 question.
How come when calling a supposedly ‘global’ function from another timeline (either external movie or parent object) this function which calls other ‘helper’ functions (within the same object as it) cannot use the helper functions via relative pathing??? i.e. this.helperFunction(vlaue1, vlaue2)

Short example…

Scenario: A menu item within menu_mc (path _root.menu_mc) is pressed


//_root.menu_mc

print_btn.onPress = function(){
  Actions.printName();
}



//_root
_global.Actions = Object();
Actions.getName = function(person){
 return person.name;
}
Actions.printName = function(){
  trace(this.getName(person))
}

The above will not work!

instead to get it to work I must remove this and call the getName() function via Actions.getName()…that just doesnt semm right to me :h:

well I didnt read through all your code but above with printNane, there is no person to reference, no matter where you use it. the only place person exists is as an argument passed into getName within the getname function block. This is not at all accessible from printName in any way.

:)… Yeah Sen that was just an example…The person object doesnt exist, i was just
trying to illustrate my question. Lets say hypothetically person does exist…I still find
that printName will not work. a call to getName will returns undefined.

Do you understand my question now?

ooo… no I dont understand heh… because if person was a real reference with a name property, that should work fine :-/

Okie dokie, well I know it should work, but it doesnt if there are multiple calls.
Maybe bringing this closer to “home” you’ll catch on. Remember the XML menu u did Sen?
And then Lost went and reworked it into a another version with drop-downs and rotation arrows?
Well anyway, in your version you created an Object call Actions like so


//your actions handlers

Actions = Object();

Actions.message = function(msg){

message_txt.text=msg;

}

ring a bell!?

And within the menu, you have the items calling the various functions with onRelease events.


//some menu item onRelease

//this.action and this.variables being properties of the menu item mc pressed set with values from XML doc
Actions[this.actions[this.variables]


Now…going back to the Person example above, if you add the extra step of using a helper function



_global.Actions = Object();

//helper function
Actions.getName = function(person){
  return person.name;

};

Actions.printName = fuction(){

//calling via relative pathing
 trace(this.getName(person));

};


You will find that the first call to printName() will work, and from there on in
it won’t because for some reason getName() becomes undefined…you must use absolute pathing to get it to work

still not following you. Can you whip up a basic file demonstrating the problem? (with working references - or what you think should work ;))

btw, the menu with the rotating arrows both lost and I did together and the menu tutorial with the Actions object came after that :wink:

OK, ill get right onto whipping up an example… Thanks

Oh yes thats right the credits ;)…Talking about Lost…havent seen him in a while

he’s lost. What more is there to say :lol:

Here it is Sen :)…All files included.

Goto line 113 of main.fla and you’ll see im calling by absolute path, now try relative and u will soon understand the source of my frustration :red:

wait, 113? Thats not basic :trout:

Heh the scripting is spaced out, its mostly yours anyway :rabbit:

Please almighty Sen can you hlp me ?