1

All code which is linked to ArrayList/List i have:

public void draw(Graphics2D gImages) {
    List missiles = aiShooter.getMissiles();

    if(keyH.shooting) {
        for(int i = 0; i < missiles.size(); i++) {
            Missile mis = (Missile) missiles.get(i);
            System.out.println("Shot");
            aiShooter.fire();
            mis.move();
            gImages.fillRect(mis.X, mis.Y, mis.Width, mis.Height);
        }
    }
    gImages.fillRect(X2, Y2, Width, Height);
    gImages.fillRect(X, Y, Width, Height);
}
package testGames2;

import java.util.ArrayList;
import java.util.List;

public class AIShooter {

    public static int X = 200;
    public static int Y = 50;
    public static int Width = 10;
    public static int Height = 3;
    List<Missile> missiles new ArrayList<>();

    public List<Missile> getMissiles() {
        return missiles;
    }

    public void fire() {
        missiles.add(new Missile(Missile.X, Missile.Y, Missile.Width, Missile.Height));
    }
}


package testGames2;

public class Missile {

    public static int X = AIShooter.X;
    public static int Y = AIShooter.Y;
    public static int Width = 10;
    public static int Height = 3;

    public Missile(int X, int Y, int Width, int Height) {
        this.X = X;
        this.Y = Y;
        this.Width = Width;
        this.Height = Height;
    }

    public void move() {
        X += 10;
    }

}

I either Can't add Missile to List, or rectangle(Missile) doesn't drawing. I tried several methods, but none of them worked. If more code is needed, just tell me. Any help is truly Appreciated!

Reyni
  • 11
  • 3
  • JOGL (OGL) is a much better platform for game development – ControlAltDel Dec 23 '22 at 16:14
  • I'm interested only in learning Java, Thanks – Reyni Dec 23 '22 at 16:16
  • You need to use a Swing Timer to schedule the animation. See: https://stackoverflow.com/a/54028681/131872 for an example to get you started. – camickr Dec 23 '22 at 16:32
  • Sorry, but how it will help me? Could you explain this? – Reyni Dec 23 '22 at 16:34
  • You haven't posted any code that *calls* `draw()`. Btw, I don't think it's a good idea to have a method called `fire` that simply adds an element to a `List` – g00se Dec 23 '22 at 16:56
  • If you need code that calls that draw method, i'm going to post it. And what about fire method? (What should i do instead?) – Reyni Dec 23 '22 at 17:17
  • *how it will help me?* - it shows you how to iterate through an ArrayList and paint objects moving on the panel. The point if for you to understand how that code works and then implement changes in your code. – camickr Dec 23 '22 at 18:16

1 Answers1

0

Your call to fire should add the new Missile created to the missiles ArrayList. Why do you think it isn't being added?

Also, for drawing your missiles, you need to set your Graphics2D context with something like setColor(someColor) before your call to fillRect. Perhaps that context was set prior to the call to draw()?

Ouroborus
  • 16,237
  • 4
  • 39
  • 62
Stu
  • 15
  • 8
  • *You need to set your Graphics2D context with something like setColor(someColor) before your call to fillRect.* What would be wrong with the default? *Perhaps that context was set prior to the call to draw()?* Which call? - none is shown – g00se Dec 23 '22 at 18:19