Sign in to follow this  
Followers 0
icecube110

Create multiple groups - 'for' loop?

3 posts in this topic

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!

Share this post


Link to post
Share on other sites

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

icecube110 likes this

Share this post


Link to post
Share on other sites
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!

breed likes this

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.