Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

1 decade ago by Nanolucas

Hey guys,

What I want is to be able to load a particular level inside main.js without it being part of the requires().

I'm using node.js and socket.io to connect users and load data into main.js, but until they've successfully connected, I won't know which level I'll be loading. If I have 20 different levels then it seems impractical to require them all into main.js if I'm only going to end up using one of them.

So please tell me, how can I do something like:

ig.require('game.levels.arena1');
ig.game.loadLevel(ig.global['LevelArena1']);

I've been searching for a while but couldn't find a solution to this, any help would be most appreciated.

1 decade ago by Joncom

Use jQuery to make an AJAX call with a callback.

var request = $.ajax({

  	url: 'http://localhost/levels/level3.js',

  	type: "GET",

  	dataType: "text"

});

request.done(function(data) {

  	console.log('AJAX call succeeded: ' + data);

        // data contains the source of the .js level
        // you just loaded. Parse it into an object
        // and then ig.game.loadLevel( object );

});

request.fail(function(jqXHR, textStatus) {

	console.log('AJAX call failed: ' + textStatus);

});

1 decade ago by Nanolucas

Thank you very much, I was able to get this working perfectly like this:

requireLevel: function(levelName, callback) {
	var _this = this;

	$.getScript('lib/game/levels/' + levelName + '.js')
	.done(function() {
		var levelObjectName = levelName.replace(/^(\w)(\w*)/, function(m, a, b) {
			return 'Level' + a.toUpperCase() + b;
		});
		
		_this.loadLevel(ig.global[levelObjectName]);
		
		if (typeof callback === 'function') {
			callback();
		}
	})
	.fail(function() {
		ig.log('Could not load ' + levelName);
		_this.handleError('Failed to load level');
		return;
	});
},

1 decade ago by Joncom

I was unaware of jQuery&039;s #getScript method. How useful.

Thanks for sharing!

1 decade ago by ad34

Hi,

i tried the Nanolucas solution, but it fails to get "LevelLevel1"

ReferenceError: LevelLevel1 is not defined

I tried several method:

_this.loadLevel(ig.global["LevelLevel1"]);

_this.loadLevel(LevelLevel1);

_this.loadLevel(ig.global[LevelLevel1]);

the variable name is correct, check here : http://www.crabegame.com/lib/game/levels/level1.js

1 decade ago by ad34

looks like some requires in the level I was loading was not loaded before, the .defines(function() was never called.

problem fixed by preloading the requires
Page 1 of 1
« first « previous next › last »