Jump to content

NOT SOLVED : this.game.state.start('level[1]')


espace
 Share

Recommended Posts

If you want to pass this.number into your state, you can pass it like this:

this.number = 1;
var clearWorld = true;
var clearCache = false;
// The first 3 parameters relate to how Phaser will start the state.
// All following parameters will be passed to the state's init function
this.game.state.start('level', clearWorld, clearCache, this.number);

In your state you can access it like this:

// Assumes this function is set as the init function of the 'level' state
function init(number) {
    // Number now holds the value of this.number that you passed in
    console.log(number);
}

(Trying for the life of me to get this syntax highlighting to work. Doesn't seem to want to cooperate. If any staff member reads this and can fix this, go for it!)

Reference:

https://phaser.io/docs/2.6.2/Phaser.StateManager.html#start

Link to comment
Share on other sites

hi Zeterain

thanks for your solutions but that don't work. 

in fact i have make a loop for my level 

var level ={}

for (var i=0; i < 5; i++) {

level

}

so if i need to reach my level i do this 
 

this.game.state.start('level[0]')

how do you concatenate to have this ?

Link to comment
Share on other sites

why not

// add level loop
for (var i = 0; i < something; i++){
 thing = 'level'+i;
}

//later reference it as
game.state.add('level'+i);

if you absolutely need an array of levels for convenience, you could always give to each level a 'key'

level[i].key = 'level'+i;

and then reference it that way.

Link to comment
Share on other sites

hi both :),

always don't work. i put an explanation :


//DON'T WORK
//level_nr = 0
on_level_selected:function(level_nr){
this.game.state.start('level'+level_nr)
}

for (var i=0; i< 2;i++){
game.state.add('level'+i,level+i)
}
//Phaser.StateManager - No state found with the key: level0
//console.log('level'+i) >> level0 
//problem is this.game.state.start(key,state)
//key is easy to concatenate
//state is a reference to an object eg: level1
//so if i a put level+i , level is not recognize as an object and i have an error

///////////////////////////////////////////////////////////////////////////////////////

//WORK but imagine with 40 levels !
//level_nr = 0
on_level_selected:function(level_nr){
this.game.state.start('level'+level_nr)
}

game.state.add('level0',level0)
game.state.add('level1',level1)
game.state.add('level2',level2)



 

Link to comment
Share on other sites

var states  = [];

states.push( new State(/* stuff goes here */ );

states.push( new State(/* stuff goes here */ );

for (var i=0; i< states.length; i++){
 game.state.add('level'+i, states[i]);
}



//somewhere else in your code
this.game.state.start('level'+level_nr)

alternatively, assuming that this refers to an instance of a class:

//... other code

this.level1 = /*..create a level here*/;
this.level2 = /*..create a level here*/;
this.level3 = /*..create a level here*/;

for(var i=0; i< someLength; i++){
  game.state.add('level'+i, this['level'+i])
}


 

Link to comment
Share on other sites

Thanks I understand very well the first proposition but for the second I don't understand how to refer "this" to an instance of the state....

Actually, I have all my states in one file, is it possible to do an instance with the second proposition ?

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...