简体   繁体   中英

Why isn't it rendering more than one of the same object?

I am trying to create 10 bricks using a LinkedList and rendered them randomly to the screen. Why isn't it working? I have been trying to figure it out for 3 days now please give me the answer. I'd really appreciate it. Thank you.

Game.java

public class Game{
  private Controller c;

  public void init(){
    c = new Controller(this);
  }

  public void run(){
    init();
    //gameLoop
  }

  public void tick(){
    c.tick();
  }

  public void render(){
   c.render(g);
  }



}

Bricks.java

public class Bricks {

private double x, y;

Game game;
private Image BrickImg;


public Bricks(double x, double y, Game game) {
    this.x = x;
    this.y = y;
    this.game = game;

    ImageIcon bricksImg = new ImageIcon("res\\bricks.png");
    BrickImg = bricksImg.getImage();
}

public void tick() {

}

public void render(Graphics g) {
    g.drawImage(BrickImg, (int)x, (int)y, null);
}

}

Controller.java

public class Controller {


Game game;

private LinkedList<Bricks> b = new LinkedList<Bricks>();

Bricks TempBricks;
Random random = new Random();

public Controller(Game game) {
    this.game = game;

    for (int i = 0; i < 10; i++) {
        addBrick(new Bricks(random.nextInt(500), 50, game));
    }
}

public void tick() {
    for (int i = 0; i < b.size(); i++) {
        TempBricks = b.get(i);
    }

    TempBricks.tick();
}

public void render(Graphics g) {
    for (int i = 0; i < b.size(); i++) {
        TempBricks = b.get(i);
    }

    TempBricks.render(g);
}

public void addBrick(Bricks brick) {
    b.add(brick);
}

public void removeBrick(Bricks brick) {
    b.remove(brick);
}

}

Sorry, but these methods make no sense:

public void tick() {
    for (int i = 0; i < b.size(); i++) {
        TempBricks = b.get(i);
    }

    TempBricks.tick(); // ticks the **last** brick in the list
}

public void render(Graphics g) {
    for (int i = 0; i < b.size(); i++) {
        TempBricks = b.get(i);
    }

    TempBricks.render(g); // renders only the **last** brick in the list
}

You iterate through the list but only act on the last one -- crazy. Why not act on the items within the for loop?:

public void tick() {
    for (int i = 0; i < b.size(); i++) {
        b.get(i).tick();  // ticks **every** brick
    }
}

public void render(Graphics g) {
    for (int i = 0; i < b.size(); i++) {
        b.get(i).render;   // renders **every** brick
    }
}

Also as cricket aptly suggests: get rid of the TempBricks field as all it is doing is confusing you.

As an aside, you will want to learn and use Java naming conventions . Variable names should all begin with a lower letter while class names with an upper case letter. Plus, a Brick is a singular object, and the class should be named as such. Learning this and following this will allow us to better understand your code, and would allow you to better understand the code of others.

Your question suggests that you're not properly debugging your program, and you will benefit greatly by using your IDE's debugger and stepping through the code, seeing what it's doing. Also debug on paper -- walk through your code logically to see if it makes sense.

You only have one TempBricks .

public class Controller {

    Game game;
    private LinkedList<Bricks> b = new LinkedList<Bricks>();

    Bricks TempBricks; // Remove this

And, as a consequence, you only use that one in your loops.

Here's a general shortcut you can use. A for-each loop.

public void render(Graphics g) {
    // renders **every** brick
    for (Brick brick : b) {
        brick.render(g);   
    }
}

Also, for removeBrick to work correctly, you must implement equals() and hashcode() in your Brick class

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM