Student Game Collider Help

Link to the project or level: Game Lab - Code.org

What I expect to happen: When projectiles hit enemies, they should displace–collider not working properly. Another option would be to set enemies to respawn once projectile hits them. I thought this might be easier to create once and have it apply to all enemies, but not sure how to do that.

What actually happens: Projectiles pass through enemies

What I’ve tried: Not sure what to try

Good Morning @Flynn_Lives,

The issue is when you shoot the bullet. It can only displace when you have fired said bullet because it’s not in an actual loop and is only activated when fired. The simple solution would be to move out the displacement outside the shooting if statement. However, this would cause it to be out of scope. This now leads to my point of iteration.

Iteration is when you loop through all the items in an array to apply properties to them (basically).
Here’s my fix:

  1. Create an array (an array is basically just a group)
var bullets = []
  1. When the bullet is being fired, add the bullet to the array with .push()
if (keyDown("space")) {
  var animation1 = createSprite(attatchment.x, attatchment.y);
  animation1.setAnimation("animation1");
  animation1.setSpeedAndDirection(-10, dir);
  animation1.setCollider("rectangle");
  bullets.push(animation1); //it should look like this
}
  1. Now create a for loop in the draw loop to check all the items in the array constantly:
for (var b = bullets.length-1; b > -1; b--) { //now this is a little harder to explain, but it's basically like: for every bullet in the array checked do this

}
  1. Now you have to create the variable to interact with your sprites one-on-one and put your displacement.
for (var b = bullets.length-1; b > -1; b--) {
    var bb = bullets[b]; //this variable gets the bullet being checked
    bb.displace(enemy);
}

If you have any questions, feel free to ask!

Example here: Game Lab - Code.org

Additionally, I have a similar post you can reference to: How to create clones in gamelab - #3 by pluto