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

10 years ago by Cacho

Hi, i need to put all the levels files requires in one line.

Eg.

I want to change this

ig.module( 
	'game.main' 
)
.requires(
	'impact.game',
	'game.levels.level_1',
        'game.levels.level_2',
	'game.levels.level_3',
	'game.levels.level_4',
	'game.levels.level_5',
	'game.levels.level_6',
	'game.levels.level_7',
	'game.levels.level_8',
	'game.levels.level_9',
	'game.levels.level_10'
)

for something like this

ig.module( 
	'game.main' 
)
.requires(
	'impact.game',
	'game.levels.*',
)

Its posible to do something?
I´m doing a game with 100 levels and i dont want to write the 100 calls.

Thancks

10 years ago by Apiheld

You can do stuff like that very easily on the command line. This is a bit more sophisticated text editing.

Run this on the shell in OSX or Linux:

for I in `seq 1 100`; do echo "'game.levels.$I',"; done

Copy & Paste, done.

10 years ago by Cacho

""Quote from Apiheld
You can do stuff like that very easily on the command line. This is a bit more sophisticated text editing.

Run this on the shell in OSX or Linux:

for I in `seq 1 100`; do echo "'game.levels.$I',"; done

Thancks Apiheld flor yogur answer. But i don't want my code have a lot of similar lines of code.
It is posible to do some while loop Lite this?:

ig.module( 
    'game.main' 
)
.requires(
    'impact.game',
    for(i=0;i<100;i++){
         'game.levels.level_'+i;
    }
)


Thancks.

10 years ago by Apiheld

It is posible to do some while loop Lite this?:


No, this doesn&039;t work. #requires() accepts only an array of strings.

Also, just calling something like 'game.levels.*' doesn't work, because JavaScript can't access the file system directly. There is no way to read existing files from the file system. You can only specify files that should be read and should exist.

You could rewrite the requires() function in impact.js (~line 245) to accept an array with multiple parameters, but this seems to be a bit over the top for just trying to avoid a few lines of code.

Two more solutions:

1. Write all requirements in a single line of code. This is less distracting in your IDE. Like so: requires('game.levels.level_1','game.levels.level_2',...

2. Make a module that has all levels in it and nothing else. In your main.js, you just include this new module:

.requires('game.all-levels')

And then you make a file all-levels.js:

ig.module( 
    'game.all-levels'
)
.requires(
    'game.levels.level_1',
    'game.levels.level_2',
    /// more ...
.defines(function() {
    AllLevels = ig.Class.extend({ /* does nothing */ });
});

10 years ago by Cacho

Thai you very much Apiheld. I'm going to trie this!
Page 1 of 1
« first « previous next › last »