Only one of the objects in my ArrayList is doing what it's supposed to
in
Programming Questions
•
5 months ago
I'm making a game in which some enemies shoot bullets at the player. I have an ArrayList of enemy objects and they should all be shooting bullets. However, when I run my program, only one of them actually does. The others still move around, but nothing else.
EDIT: what is supposed to happen is.. there are 3 enemies on screen. Every second, a bullet is generated at their coordinates at that moment in time, and it is displayed and updated (it moves towards the player).
Relevant code (there is much more code than this, but this is everything involving enemies):
EDIT: what is supposed to happen is.. there are 3 enemies on screen. Every second, a bullet is generated at their coordinates at that moment in time, and it is displayed and updated (it moves towards the player).
Relevant code (there is much more code than this, but this is everything involving enemies):
- ArrayList <Enemy> eList2 = new ArrayList();
- Enemy newEn;
- Enemy en2;
- void setup() {
- size(800, 800);
- for (int i = 0; i < num2; i++) {
- Enemy newEn2 = new Enemy(random(TWO_PI), (int) random(-1, 2), random(PI/300, PI/200));
- eList2.add(newEn2);
- }
- }
- void draw() {
- for (int i = 0; i < eList2.size(); i++) {
- en2 = eList2.get(i);
- pushMatrix();
- translate(width/2, height/2);
- en2.display();
- popMatrix();
- en2.update(2);
- }
- // BULLETS
- //bulletVel.set(pl.pos.x-width/2, pl.pos.y-height/2, 0);
- if (frameCount % 60 == 0) {
- ///////////////////////// NOTE: the following line creates a bullet at the enemy coordinates, so it might be relevant
- bu = new Bullet(en2.pos, bulletVel);
- bList.add(bu);
- }
- println(en2.pos.x);
- for (int i = 0; i < bList.size(); i++) {
- Bullet fireball = bList.get(i);
- pushMatrix();
- translate(width/2, height/2);
- fireball.render();
- fireball.update();
- fireball.addBullet();
- popMatrix();
- }
- }
Enemy class
- class Enemy extends Object {
- float angle, da, amplitude;
- Boolean hit;
- // level one constructor
- Enemy (float x, float y) {
- pos = new PVector(x, y);
- vel = new PVector(random(-3, 3),random(-3, 3));
- angle = 0;
- w = h = 30;
- hit = false;
- }
- // level two constructor
- Enemy (float a, int direction, float speed) {
- pos = new PVector(width/2, height/2);
- w = h = 20;
- angle = a;
- amplitude = 250;
- hit = false;
- if (direction <= 0) {
- da = speed;
- }
- else {
- da = -speed;
- }
- }
- // level one
- void render() {
- stroke(0);
- fill(col);
- pushMatrix();
- translate(pos.x, pos.y);
- rectMode(CORNER);
- imageMode(CORNER);
- image(swordUp, 0, 0);
- popMatrix();
- }
- // level two
- void display() {
- stroke(0);
- fill(col);
- pushMatrix();
- translate(pos.x, pos.y);
- imageMode(CENTER);
- image(alduin, 0, 0, 96, 96);
- popMatrix();
- }
- void update(int level) {
- if (level == 1) {
- pos.add(vel);
- // wall collision
- if (pos.x < 0 || pos.x > width-w) {
- vel.x *= -1;
- }
- if (pos.y < 0 || pos.y > sb1.pos.y-h) {
- vel.y *= -1;
- }
- }
- if (level == 2) {
- pos.x = amplitude* cos(angle);
- pos.y = amplitude* sin(angle);
- angle += da;
- }
- }
- }
Bullet class
- class Bullet extends PVector {
- PVector vel;
- Bullet(PVector loc, PVector vel) {
- super(loc.x, loc.y);
- this.vel = vel.get();
- }
- void render() {
- pushMatrix();
- translate(x, y);
- image(fireball,0, 0);
- popMatrix();
- }
- void update() {
- add(vel);
- }
- void addBullet() {
- bulletVel.set(pl.pos.x-width/2, pl.pos.y-height/2, 0);
- bulletVel.sub(en2.pos);
- bulletVel.normalize();
- bulletVel.mult(4);
- }
- }
1