Last active
August 29, 2015 14:24
-
-
Save hpstuff/c83197fde40b80b79fc3 to your computer and use it in GitHub Desktop.
Loader
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var EventDispatcher = (function(){ | |
| function EventDispatcher(){ | |
| this._listeners = {}; | |
| } | |
| EventDispatcher.prototype.dispatchEvent = function(event, data){ | |
| var _this = this; | |
| if(!this._listeners || !this._listeners[event]) return; | |
| this._listeners[event].forEach(function(callback){ | |
| callback.call(_this, data); | |
| }); | |
| }; | |
| EventDispatcher.prototype.addEventListener = function(event, listener){ | |
| if(!this._listeners) this._listeners = {}; | |
| if(!this._listeners[event]) this._listeners[event] = []; | |
| this._listeners[event].push(listener); | |
| }; | |
| EventDispatcher.prototype.removeEventListener = function(event, listener){ | |
| if(!this._listeners[event]) return; | |
| var index = this._listeners[event].indexOf(listener); | |
| if(index > -1) this._listeners[event].splice(index, 1); | |
| }; | |
| EventDispatcher.implementation = function(proto){ | |
| var prop; | |
| for(prop in EventDispatcher.prototype){ | |
| proto[prop] = EventDispatcher.prototype[prop]; | |
| } | |
| }; | |
| return EventDispatcher; | |
| })(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var Loader = (function(parent){ | |
| function Loader(){ | |
| this.data = {}; | |
| this.imagesData = []; | |
| } | |
| Loader.prototype = new parent; | |
| Loader.prototype.constructor = Loader; | |
| Loader.prototype.loadImages = function(images){ | |
| var _this = this; | |
| this.imageCounter = 0; | |
| (this.imagesData = images).forEach(function(image){ | |
| _this.loadImage(image); | |
| }); | |
| }; | |
| Loader.prototype.loadImage = function(data){ | |
| var _this = this, image = new Image(); | |
| if(this.imagesData.length === 0) this.imagesData.push(data); | |
| if(this.imageCounter == void 0) this.imageCounter = 0; | |
| image.onload = function(){ | |
| _this.data[data.id] = this; | |
| _this.imageCounter++; | |
| _this.dispatchEvent("update", _this.imageCounter); | |
| if(_this.imageCounter === _this.imagesData.length){ | |
| _this.imagesData.length = 0; | |
| _this.imageCounter = void 0; | |
| _this.dispatchEvent("complete") | |
| } | |
| }; | |
| image.src = data.src; | |
| }; | |
| Loader.prototype.getResult = function(id){ | |
| return this.data[id]; | |
| }; | |
| return Loader; | |
| })(EventDispatcher); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var loader = new Loader(); | |
| loader.addEventListener("complete", function(){ | |
| }); | |
| loader.loadImages([ | |
| {id: "sBackground", src: "images/Background/starBackground.png"}, | |
| {id: "player", src: "images/player.png"}, | |
| {id: "gLaser", src: "images/laserGreen.png"}, | |
| {id: "gLaserShot", src: "images/laserGreenShot.png"}, | |
| {id: "enemyShip", src: "images/enemyShip.png"}, | |
| {id: "shield", src: "images/shield.png"}, | |
| {id: "playerDamaged", src: "images/playerDamaged.png"}, | |
| {id: "life", src: "images/life.png"}, | |
| {id: "playerExplosion", src: "images/playerExplosion.png"}, | |
| {id: "explosion", src: "images/explosion.png"} | |
| ]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment