Only one sprite will get hit by the players attack

When ever my player attacks, the hitbox only affects enemy1 and it doesn’t effect any other enemies

The game
What I expect to happen: When the player attacks, all enemies get hit and they lose hp
What actually happens: When the player attacks, only enemy1 gets hit and loses hp

var bg = createSprite(200, 200);
bg.setAnimation("placeholder_bg");
bg.scale = 0.8;
var ply = createSprite(200, 300);
ply.setAnimation("Placeholder_plywalkleft");
ply.setCollider("rectangle", 0, 25, 20, 20);
ply.debug = true;
var enemy1 = createSprite(50, 350);
enemy1.setAnimation("placeholder_enemy");
enemy1.setCollider("rectangle", 0, 0, 30, 30);
enemy1.hp = 5;
enemy1.debug = true;
var enemy2 = createSprite(350, 300);
enemy2.setAnimation("placeholder_enemy");
enemy2.setCollider("rectangle", 0, 0, 30, 30);
enemy2.hp = 5;
enemy2.debug = true;
var enemy3 = createSprite(350, 350);
enemy3.setAnimation("placeholder_enemy");
enemy3.setCollider("rectangle", 0, 0, 30, 30);
enemy3.hp = 5;
enemy3.debug = true;
var enemy4 = createSprite(50, 300);
enemy4.setAnimation("placeholder_enemy");
enemy4.setCollider("rectangle", 0, 0, 30, 30);
enemy4.hp = 5;
enemy4.debug = true;
var bar = createSprite(200, 197);
bar.setAnimation("invisbar");
bar.debug = true;
var hitbox = createSprite(450, 450, 70);
hitbox.debug = true;
var coincount = createSprite(30, 25);
coincount.setAnimation("CoinCount");
coincount.scale = 1.6;

// groups //


// general values //
// var enemy1hp = 5;
// var enemy2hp = 5;
// var enemy3hp = 5;
// var enemy4hp = 5;
var enestartpos1x = 50;
var enestartpos1y = 350;
var enestartpos2x = 350;
var enestartpos2y = 300;
var enestartpos3x = 350;
var enestartpos3y = 350;
var enestartpos4x = 50;
var enestartpos4y = 300;
var possetover = false;
var facingL = true;
var facingR = false;
var cdcounter = 0;

// math values and stuff //
var enemy1velocity = 1.3;
var enemy2velocity = 1.3;
var enemy3velocity = 1.3;
var enemy4velocity = 1.3;
var deltaX = 0;
var deltaY = 0;

// item stat stuff bc code.org doesnt give us tables in gamelab //

// base collectables //
var coins = 0;

// base stats //
var plyhp = 6;
var speed = 3;
var firerate = 1;
var dmg = 3;
var luck = 0;

function draw() {

  plyController();
  enemyController(enemy1, enemy1velocity, enestartpos1x, enestartpos1y);
  enemyController(enemy2, enemy2velocity, enestartpos2x, enestartpos2y);
  enemyController(enemy3, enemy3velocity, enestartpos3x, enestartpos3y);
  enemyController(enemy4, enemy4velocity, enestartpos4x, enestartpos4y);
  // hides barrier after all enemy collision checks are called
  hitbox.x = 450;
  hitbox.y = 450;
  bar.displace(ply);
  bar.displace(enemy1);
  bar.displace(enemy2);
  bar.displace(enemy3);
  bar.displace(enemy4);

  drawSprites();
}

function plyController() {
  if (keyDown("w")) {
    ply.y += -3;
  }
  if (keyDown("s")) {
    ply.y += 3;
  }
  if (keyDown("a")) {
    ply.x += -3;
    facingL = true;
    facingR = false;
    ply.setAnimation("Placeholder_plywalkleft");
  }
  if (keyDown("d")) {
    ply.x += 3;
    facingR = true;
    facingL = false;
    ply.setAnimation("Placeholder_plywalkright");
  }
  if (keyWentDown("space")) {
    if (facingL == true) {
      hitbox.y = ply.y;
      hitbox.x = ply.x - 70;
    } else {
      if (facingL == true) {
        hitbox.y = 450;
        hitbox.x = 450;
      }
    }
    if (facingR == true) {
      hitbox.y = ply.y;
      hitbox.x = ply.x + 70;
    } else {
      if (facingR == true) {
        hitbox.y = 450;
        hitbox.x = 450;
      }
    }
  }
}
// also since this is isn't low level programming you cannot pass a variable like Number as a pointer
// instead a pseudo pointer is made from an Object / Array datatype, since your already have a Sprite <object>
// i'll just append the .hp attribute to all enemies so it updates correctly
function enemyController(enemy, enemyvelocity, enerestarx, enerestary) {
  if (cdcounter == 0) {
    if (enemy.collide(ply)) {
      plyhp -= 1;
      // console.log(plyhp);
      playSound("sound://category_explosion/retro_game_take_damage_chirp_3.mp3", false);
      cdcounter += 1;
    }
  } else {
    if (cdcounter >= 100) {
      cdcounter = 0;
    } else {
      cdcounter += 1;
    }
  }
  enemy.pointTo(ply.x, ply.y);
  follow(enemy, ply, enemyvelocity);
  if (enemy.isTouching(hitbox)) {
    console.log('enemy hit');
    enemy.hp -= 1;
    // console.log(enemy.hp);
    if (enemy.hp < 0) {
      enemy.x = enerestarx;
      enemy.y = enerestary;
      enemy.hp = 5;
    }
    // you could make it so the hitbox only collides with 1 enemy though
  }
  // bad because this will make it remove the attack if the first enemy hasn't collided with it
  // hitbox.y = 450;
  // hitbox.x = 450;
}

function follow(follower, followed, velocity) {
  //the follower follows the followed at a constant velocity
  deltaX = followed.x - follower.x;
  deltaY = followed.y - follower.y;
  var followerAngle = Math.atan(deltaY / deltaX);
  if (deltaX < 0) {
    // the arctan assumes that the angle is in the first or 
    // fourth quadrants, so if it's in the second or third
    // (i.e. deltaX/cosign is negative) correct by adding PI
    followerAngle = followerAngle + Math.PI;
  }
  follower.velocityX = velocity * Math.cos(followerAngle);
  follower.velocityY = velocity * Math.sin(followerAngle);
}

a lot of stuff wrong i’d suggest reading the comments i left… if not your more than welcome to test it on your own to see what was wrong

Hi @kathrynpanzarella,

Variance’s suggestions are always amazing and knowledgable. You should definitely look at those comments.

I want to explain how I usually create multiple sprites (enemies) and check for collisions using for loops. By using a for loop in the draw loop to continually check for collision, you ensure each enemy is constantly being checked. Variance had an answer for why your multiple function calls with to each enemy didn’t work and a for loop is one solution.

I created a VERY SIMPLE:) example from your game. Starting on line 14, I created 4 enemies using a for loop. Then on line 91, I use a for loop to constantly check for collision and reset the x. Using this technique, I’m guessing you can apply your more advanced controllers if you feel this works for you.

I hope that helps!
~Michelle