How to create lots of simmilar enemies

I want to create a function that can create enemies that behave the same but they are in different x and y positions. I feel I am overthinking it. this is my previous attempt that did not work because the console says the enemy parameter is undefined after the first frame.

    //creating the functions for the enemies
    //grass monster functions
    const createGrassMonster = (xPosition, yPosition, enemy) => {
        if (start == true)    //start is equal to true only if it is the first frame of the game
        {
            enemy = this.physics.add.sprite(xPosition, yPosition, 'wood');
            this.physics.add.collider(enemy, this.player);
        }
        //here is the part where you would move the enemy
        //or things you would normally do in the update function
    }
    window.setInterval(createGrassMonster, 1); 
    //this line of code above calls the function on repeat


   //this is where I call my function for the enemy
  // wont work if it is in the update function
   createGrassMonster(100, 100, this.woodMonster); 
   createGrassMonster(20, 20, this.woodMonster2);

what I am thinking is that when you make a new group you could do this

this.physics.add.group({
    key: 'blah',
    repeat: 10,
    setXY: etc...
})

what if I were to make my own function like key: or repeat: and add that function to a group to make it act like I want my enemy to act. If someone out there knows how to do this that would be great.

or perhaps I could add a function that applies to all the children of a group. that would be even better.

AHA!

enemies = this.physics.add.group(); // this is in create function

//everything below is in update function
enemies.children.iterate(function (child) {
    //make the enemies move below
    
});

i was overthinking it too much :chair:

1 Like