diff --git a/scripts/game.js b/scripts/game.js index 3b341a6..d2dde29 100644 --- a/scripts/game.js +++ b/scripts/game.js @@ -24,8 +24,11 @@ /* jshint browser:true */ /* global lasertank */ -const load_ltg = lasertank.FileLoader( document.getElementById( 'ltg' ) ), - load_lvl = lasertank.FileLoader( document.getElementById( 'lvl' ) ), +const ltg_input = document.getElementById( 'ltg' ), + lvl_input = document.getElementById( 'lvl' ), + + load_ltg = lasertank.FileLoader( ltg_input, new window.FileReader() ), + load_lvl = lasertank.FileLoader( lvl_input, new window.FileReader() ), ele_game = document.getElementById( 'game' ), ctx = document.getElementById( 'render' ).getContext( '2d' ); diff --git a/src/FileLoader.js b/src/FileLoader.js index 52bbc6b..ba63145 100644 --- a/src/FileLoader.js +++ b/src/FileLoader.js @@ -49,8 +49,9 @@ module.exports = Class( 'FileLoader', * Initialize file loader, monitoring the given file element * * @param {HtmlInputElement} element file element to monitor + * @param {FileReader} reader file reader */ - __construct: function( element ) + __construct: function( element, reader ) { if ( element.type !== 'file' ) { @@ -58,6 +59,7 @@ module.exports = Class( 'FileLoader', } this._element = element; + this._reader = reader; }, @@ -105,18 +107,18 @@ module.exports = Class( 'FileLoader', if ( files.length === 0 ) return; - const reader = new FileReader(); - reader.onload = function( revent ) + this._reader.onload = function( revent ) { _self._callback.call( this.__inst, null, revent.target.result ); }; - reader.onerror = function( e ) + + this._reader.onerror = function( e ) { _self._callback.call( this.__inst, e ); }; // load file - reader.readAsBinaryString( files[ 0 ] ); + this._reader.readAsBinaryString( files[ 0 ] ); } } );