my enemy won't move correct

edited December 2017 in Questions about Code

ok, so I thought I'd make my newer version of space invaders like space invaders the regular one & have them all move down together, but they keep just moving indivual.

here the code in enemy I think that all you need idk;

class Enemy extends Circle { float life = 1; float spd = 1; float type = 1; // here I give each class a type so I can have several different enemies Enemy(float x, float y, float r) { super(x, y, r); } void show() { fill(128, 0, 128); ellipse(x, y, r*2, r*2); } void move() { x += spd; if (x+r > width && spd > 0) { // here it checks if collide with wall for (Enemy e : enemy) { // here I want it to loop through all the enemy if (e.type == 1 && spd > 0) { //any with type = 1 need to move down spd *= -1; y += 30; } } } else if (x-r < 0 && spd < 0) { /// I havent done this one yet it for the other side spd *= -1; y += 30; } } void grow() { for (int i = enemy.size()-1; i >= 0; i--) { Enemy e = enemy.get(i); if (e.life <= 0) { enemy.remove(i); control.dead -= 1; } } } }

Tagged:

Answers

  • It doesn't really make sense to loop through all of your Enemy instances inside your Enemy class. An instance of the Enemy class represents a single enemy. If you want to loop through them all, that loop should be outside the class.

  • I want when the first one hits the edge for all of type 1 to move down. Wouldnt i need to loop through them to move all of type 1 down?

  • Answer ✓

    yes but outside the class. That's the point

Sign In or Register to comment.