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
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:
Create an array (an array is basically just a group)
var bullets = []
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
}
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
}
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);
}