3man7 Posted April 19, 2017 Share Posted April 19, 2017 Hey guys, I have a quick question about 'Groups' as I'm kinda new to this Phaser HTML5 game dev. So my plan is to create multiple groups and have control over each one of them. First thing that came into my mind is to create a 'for loop' and be done with! Problem is that I don't know how to assign different names for a variable (wave).Example: Instead of doing this: this.wave00 = game.add.group(); this.wave00.enableBody = true; this.wave00.physicsBodyType = Phaser.Physics.ARCADE; this.wave01 = game.add.group(); this.wave01.enableBody = true; this.wave01.physicsBodyType = Phaser.Physics.ARCADE; this.wave02 = game.add.group(); this.wave02.enableBody = true; this.wave02.physicsBodyType = Phaser.Physics.ARCADE; I am trying to do this: for(i=0; i<10; i++){ this.['wave'+i] = game.add.group(); this.['wave'+i].enableBody = true; this.['wave'+i].physicsBodyType = Phaser.Physics.ARCADE; } So I'm sure you got the idea but this (['wave'+i]) is the problem. Can someone help me with this? What is the correct way of writing it? I've been searching but maybe I'm not questioning this correctly. Thanks and sorry for the noob question! Link to comment Share on other sites More sharing options...
snowbillr Posted April 20, 2017 Share Posted April 20, 2017 So close! when you're using the bracket syntax, you don't put a dot before it. So it should be this['wave'+i].whatever 3man7 1 Link to comment Share on other sites More sharing options...
3man7 Posted April 20, 2017 Author Share Posted April 20, 2017 13 hours ago, breed said: So close! when you're using the bracket syntax, you don't put a dot before it. So it should be this['wave'+i].whatever Haha nice! Yeah that was it. I knew something was off but for some reason I couldn't get it. Thanks for the help! snowbillr 1 Link to comment Share on other sites More sharing options...
Recommended Posts