Enemies

Hi guys, I have a problem adding a new enemy.
To bring up the first enemy I had no problems:

slime = this.physics.add.staticGroup();
slime = this.physics.add.sprite(300, 450, 'slime');
slime.setCollideWorldBounds(true);
this.physics.add.collider(slime, platforms);

And then I thought, to add a new slime, I write like this:

slime = this.physics.add.sprite(500, 450, 'slime');

Now I have created a second slime which however falls down and disappears from the game scene.
What can I do?

Have you read Making your first Phaser 3 game?

Yes…

 slime = this.physics.add.staticGroup({
        key:'slime',
        repeat: 4,
        setXY: { x: 300, y: 330, stepX: 70 }
    }

But… I want so set enemies in different places

ok it works.
But now how do I tell all the slimes to move like the first?

slime = this.physics.add.group({
        key: 'slime',
        repeat: 4,
        setXY: { x: 200, y: 300, stepX: 180 }
    });
    this.physics.add.collider(slime, platforms);

slime = this.physics.add.group({
  key: 'slime',
  repeat: 4,
  setXY: { x: 200, y: 300, stepX: 180 },
  velocityX: 60
});
1 Like

yes it works but if I have this function that moves the first slime?
How can I do the same thing for the others?

function slimeMove(){
    if(slime.body.touching.down){
        if(slime.direction !='left'){
            slime.setVelocityX(65);
            slime.anims.play('slimeright' ,true);
        } else {
            slime.setVelocityX(-65);
            slime.anims.play('slimeleft' ,true);
        }
        
    }

I tried to call the function but it doesn’t work…

Use slimes for the group.

var slimes;

function create() {
  slimes = this.physics.add.group({
    key: 'slime',
    repeat: 4,
    setXY: { x: 200, y: 300, stepX: 180 },
    velocityX: 60
  });
}

function update() {
  slimes.getChildren().forEach(slimeMove);
}

function slimeMove(slime) {
  if (slime.body.touching.down) {
    if (slime.direction !== 'left') {
      slime.setVelocityX(65);
      slime.anims.play('slimeright', true);
    } else {
      slime.setVelocityX(-65);
      slime.anims.play('slimeleft', true);
    }
  }
}
1 Like

It doesn’t work…