espace Posted February 18, 2017 Share Posted February 18, 2017 hi, i have this variable this.number = 1 and i want to acces this state : this.game.state.start('level'[this.number]) how do i do with the syntax to reach this level ? thanks Link to comment Share on other sites More sharing options...
Zeterain Posted February 18, 2017 Share Posted February 18, 2017 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 More sharing options...
Zeterain Posted February 18, 2017 Share Posted February 18, 2017 If you are trying to concatenate 'level' and the number 1, you can concatenate the number to your string like so: this.number = 1; this.game.state.start('level' + this.number); // Results in this.game.state.start('level1'); Link to comment Share on other sites More sharing options...
espace Posted February 18, 2017 Author Share Posted February 18, 2017 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 More sharing options...
espace Posted February 18, 2017 Author Share Posted February 18, 2017 the real problem is to add the level in game.state.add('level'+level_number,level+level_number) because the second parameter is not recognize as level1 Link to comment Share on other sites More sharing options...
ldd Posted February 18, 2017 Share Posted February 18, 2017 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 More sharing options...
espace Posted February 19, 2017 Author Share Posted February 19, 2017 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 More sharing options...
ldd Posted February 19, 2017 Share Posted February 19, 2017 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]) } espace 1 Link to comment Share on other sites More sharing options...
espace Posted February 20, 2017 Author Share Posted February 20, 2017 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 More sharing options...
Recommended Posts