Tutorials, extensions, and source files for ActionScript, Flash, and other Adobe products.

 

senocular.com ActionScript Library

loadSeq.as

Name: loadSeq() - loadVars in sequential order one after the other
Author: senocular: www.senocular.com
Date: 1899-12-31T00:17:32.900
Documentation:
LoadVars LOADSEQ: load variables in a secequential order, one
after the other into a loadVars object (or rather an array within).

Arguments:
- keep: boolean to determine whether or not the loaded variables are kept within the actual
LoadVars object itself (true) or just within its _loadedVars array (false) which is created the
first time loadSeq is used.
- args: any number of comma seperated urls to be loaded using loadSeq.

Properties: (added to the loadVars object)
- allLoaded: similar to loaded though refers to all of the urls loaded using loadSeq.  When
loadSeq is called, allLoaded is set to false.  It continues to be false until all of the urls are
downloaded successfully.  If one of the urls fails to load or the function is interrupted before
downloading all of the urls, this value will remain false.
- _loadedVars: an array containing individual loadVars objects used to contain the individually
loaded urls.  if the keep argument is false, this array is where you would retrieve your loaded
variables.

Handlers:
- onAllLoaded: handler called when the loading of the urls is complete, either with or
without error.  A success argument is provided similarly to that of loadVars.onLoad.  success is
only true when all of the urls have successfully loaded.
- onNextVar: this is called at the beginning of loading a new url (but not the first).  By
default the function only returns true.  This return value dictates whether or not to continue
loading the url sequence. If you make a custom onNextVar for your loadVars object, be sure to
include the return true (or false) so you can control or allow the loading of the next url. If a
false is returned from a onNextVar call, the success of the loading will be considered false.

Note:
- using loadSeq more than once will compound the loaded variables into whatever variables already
exist within the _loadedVars array.  If you do not want this, manually reset or delete the 
_loadedVars array of your loadVars object.
- loading variables with the same name as previously loaded variables will replace the prior 
variables in the main loadVars object if keep is true.  You can use the _loadedVars array to 
retrieve previously loaded variables loaded through loadSeq which may have similar names.

Example:
a = new LoadVars();
a.loadSeq(true, "vars1.txt", "vars2.txt", "vars3.txt");
a.onAllLoaded = function(success){
	for (prop in this){ // for all loaded variables in this object 
		if (typeof this[prop] == "string") trace(prop +": "+ this[prop]); // displays variables
	}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
LoadVars.prototype.loadSeq = function(keep, args){
	if (!this._loadedVars) this._loadedVars = [];
	this.allLoaded = false;
	var args = arguments, main = this, suc = true, pos = 0;
	var argOnLoad = function(success){
		if (keep) for (vars in this) if (typeof this[vars] == "string") main[vars] = this[vars];
		if (suc && !success) suc = false;
		if (pos < args.length-1){
			if (main.onNextVar()){
				var next = main._loadedVars[main._loadedVars.length] = new LoadVars();
				next.load(args[++pos]);
				next.onLoad = argOnLoad;
			}else main.onAllLoaded(main.allLoaded = false);
		}else main.onAllLoaded(main.allLoaded = suc);
	}
	var curr = this._loadedVars[this._loadedVars.length] = new LoadVars();
	curr.load(arguments[++pos]);
	curr.onLoad = argOnLoad;
}
LoadVars.prototype.onNextVar = function(){ return true; }