// Preloader Class v2.2 // by Jonas Galvez // // Description: Preloads content that will be loaded by one or more // instances of either MovieClip, Sound, LoadVars or XML classes // // Properties: kbTotal, kbLoaded, kbLeft, percentComplete, instances // Events: onPreloadStart, onPreloadUpdate, onPreloadComplete // // If you have any comments or suggestions about the code, // please send them to jonasgalvez@uol.com.br // Custom functions and methods used in the Preloader Class // --> hasArray Function // Checks if the first item of an array or // arguments object contains an array _global.hasArray = function(obj) { if(typeof obj[0] == "object" && obj[0].length) return obj.shift(); else return obj; }; // --> Object.isInstanceOf Method // Returns true if the object belongs to any of the specified classes Object.prototype.isInstanceOf = function() { if(arguments.length) for(i in arguments) { if(this instanceof arguments[i]) return true; }; return false; }; // --> Object.addProperties Method // Adaptation of the Object.addProperty Method // that allows you to add more than one property at a time Object.prototype.addProperties = function() { var properties = hasArray(arguments); var prop, hasGetter, hasSetter; for(property in properties) { prop = properties[property]; if(this["get" + prop] instanceof Function) hasGetter = true; if(this["set" + prop] instanceof Function) hasSetter = true; if(hasGetter && hasSetter) { this.addProperty(prop, this["get" + prop], this["set" + prop]); } else if(hasGetter) { this.addProperty(prop, this["get" + prop], null); }; hasGetter = hasSetter = false; }; }; _global.Preloader = function() { var args = hasArray(arguments); this._total = this._loaded = this.left = 0; this.total = this.loaded = this.left; this.percentage = this._started = 0; this._instances = this._tempInstances = new Array(); for(var i = 0; i < args.length; i++) { if(args[i].isInstanceOf(MovieClip, Sound, LoadVars, XML)) { args[i]._tempTotal = args[i].getBytesTotal(); ASSetPropFlags(args[i], "_tempTotal", 1); this._instances[this._instances.length] = args[i]; }; }; this._interval = setInterval(this, "_main", 100); }; Preloader.prototype.getKBTotal = function() { return this.total; }; Preloader.prototype.getKBLoaded = function() { return this.loaded; }; Preloader.prototype.getKBLeft = function() { return this.left; }; Preloader.prototype.getPercentComplete = function() { return this._percentage; }; Preloader.prototype.getInstances = function() { return this._instances; }; var props = ["kbTotal", "kbLoaded", "kbLeft", "percentComplete", "instances"]; Preloader.prototype.addProperties(props); Preloader.prototype._main = function() { this._total = this._loaded = 0; for(var j = 0; j < this._instances.length; j++) { this._total += this._instances[j].getBytesTotal(); this._loaded += this._instances[j].getBytesLoaded(); if(this._instances[j].getBytesTotal() != this._instances[j]._tempTotal) { if(!this._tempInstances[this._instances[j]] && this._instances[j].getBytesTotal() > 0) { this._tempInstances[this._instances[j]] = this._instances[j]; this._started++; }; }; }; if(this._instances.length == this._started) { this.total = Math.round(this._total / 1024); this.loaded = Math.round(this._loaded / 1024); if(!this._OPScalled) { this.onPreloadStart(); this._OPScalled = true; }; this.left = this.total - this.loaded; this._percentage = Math.round(this.loaded*100/this.total); this.onPreloadUpdate(); if(this._percentage == 100) { this.onPreloadComplete(); clearInterval(this._interval); }; }; }; ASSetPropFlags(_global, "Preloader", 1); delete props; // --> Usage Example 1 // // _root.myMovieClip.load("file.swf"); // _root.mySound.loadSound("file.mp3", false); // // myPreloader = new Preloader(_root.myMovieClip, _root.mySound); // // myPreloader.onPreloadUpdate = function() { // _root.total_kb.text = this.kbTotal + "Kb"; // _root.loaded_kb.text = this.kbLoaded + "Kb"; // _root.left_kb.text = this.kbLeft + "Kb"; // _root.percentage_loaded.text = this.percentComplete + "%"; // }; // // myPreloader.onPreloadComplete = function() { // _root.message.gotoAndStop("Loaded"); // }; // // // --> Usage Example 2 // // var instances = []; // // for(var i = 0; i < 10; i++) { // _root["photo" + i].loadMovie("file" + i + ".jpg"); // instances.push(_root["photo" + i]); // }; // // myPreloader = new Preloader(instances); // // _root.message.text = "Receiving Files Headers..."; // // myPreloader.onPreloadStart = function() { // // _root.message.text = "Loading Files..."; // // this.onPreloadUpdate = function() { // _root.progressBar._xscale = this.percentComplete; // }; // // }; // // myPreloader.onPreloadComplete = function() { // // _root.message.text = "Done!"; // // for(var i = 0; i < this.instances.length; i++) { // this.instances[i].gotoAndStop(2); // }; // // };