donpink Posted May 31, 2015 Share Posted May 31, 2015 I would like the name of the character to change at each death. How could i pick up, randomly, a name from a self made list ? this.rng = Phaser.RandomPROBLEM("ABY", "ALEX", "AL"); var name = this.game.add.text(-405, -20, this.rng, this.styleMalin);this.mainGroup.add(name); Link to comment Share on other sites More sharing options...
mwatt Posted May 31, 2015 Share Posted May 31, 2015 I think Phaser might have some built in stuff for some of what you want to do, but I'm not sure. However, pure JavaScript will do fine for this. You can stick the names in an array and then generate a random index into that array from which to pull one. Off the top of my head (warning, code is untested): var nameArray = ["Moe","Larry","Curly"];var randomIntInArrayRange = Math.floor((Math.random() * (nameArray.length -1)));var randomName = nameArray[randomIntInArrayRange]; Link to comment Share on other sites More sharing options...
XekeDeath Posted May 31, 2015 Share Posted May 31, 2015 Or use Phasers built in int generator...Assuming you have an array of names to choose from, called this.names:this.game.rnd.integerInRange(0, this.names.length-1);http://phaser.io/docs/2.3.0/Phaser.RandomDataGenerator.html#integerInRange Link to comment Share on other sites More sharing options...
ForgeableSum Posted June 1, 2015 Share Posted June 1, 2015 No need to get integers. A more direct way: var namesArray = ['Bob','Al','Tom'];var name = this.game.rnd.pick(namesArray); drhayes 1 Link to comment Share on other sites More sharing options...
Recommended Posts