ok sorry if i post way too much asking for things but...

i wanna make a enemy generation loop that can be treated like one is like their own separate variable, if you do not know what i mean i will show you

var player = createSprite(42.0, 69);
player.setAnimation("player");
var enemy1 = createSprite(69, 420);
enemy1.setAnimation("enemy");
var enemy2 = createSprite(420, 69);
enemy2.setAnimation("enemy");

var enemies = createGroup();

function draw() {
 if (enemy1.isTouching(player)) {
  console.log("touched enemy 1");
 }
if (enemy2.isTouching(player)) {
  console.log("touched enemy 2");
 }
}
//as you can see, i want a shortened version with a loop
//for example i want smth like this while you can still interact with the clones individually

pretend there was a loop here {
 var enemy = createSprite(randomNumber(50, 350), randomNumber(50, 350));
 enemy.setAnimation("enemy");
}

first, create an array
var enemies = [];
then, add a bunch of enemies to the array:
for(var i = 0; i < 10; i++){
var enemy1 = createSprite(randomNumber(50, 350), randomNumber(50, 350));
enemy1.setAnimation("enemy");
enemies.push(enemy1);
}

then, loop through the array for the enemy behaviors
function draw(){
for(var i = enemies.length - 1; i > -1; i--){
enemies[i].doAIorWhatever();
enemies[i].draw();
}
}

    ackvonhuelio Yeh that pretty much works but just to let pluto know if you are tryna create a bunch of enemies (like one hundred), its better to split it into smaller arrays since it will lag the draw loop so badly giving you 1fps or smth

    also ack is enemies.draw() built in to code.org (the draw function for everysingle sprite)?

      ASmartCoder
      oh shit i forgor about how CDO sprites work

      sorry, drawSprite(enemies[i]);

      also splitting it into smaller arrays wouldn't help, what you could do is make a chunk system and only update sprites within a couple of chunks of the player. then, when those sprites are moving, check to update what chunk that sprite is in.

      I've never actually tried chunks with moving objects, only with stationary walls, but i think it'd work

      7 days later

      pluto
      for(var i = 0; i < 10; i++){
      var enemy1 = createSprite(randomNumber(50, 350), randomNumber(50, 350));
      enemy1.setAnimation("enemy");
      enemy1.health = 100;
      enemy1.weapons = ['sword','gun'];
      enemies.push(enemy1);
      }

      function draw(){
      for(var i = enemies.length - 1; i > -1; i--){
      enemies[i].doAIorWhatever();
      if(enemies[i].wasHitOrSomething){
      enemies[i].health -= 1;
      }
      enemies[i].draw();
      }
      }

        ackvonhuelio sorry if i have a lot of questions, but how do i make another array interact with another array with your method?

          ackvonhuelio ok so like

          imagine there is 2 of them

          for loop #1
          and for loop #2

          and they do the same thing, they loop through sprite behaviors, but i cant get them to interact like for example

          if (enemy[i].isTouching(target[i])) {
           console.log("42069")
          }

            pluto
            for(var i = 0; i < enemies.length; i++){
            for(var q = 0; q < targets.length; q++){
            if (enemies[i].isTouching(targets[q])) {
            console.log("42069")
            }
            }
            }

            Chat