简体   繁体   English

为什么它不能渲染多个同一个对象?

[英]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. 我正在尝试使用LinkedList创建10块积木,并将它们随机呈现到屏幕上。 Why isn't it working? 为什么不起作用? I have been trying to figure it out for 3 days now please give me the answer. 我已经尝试解决了3天,请给我答案。 I'd really appreciate it. 我真的很感激。 Thank you. 谢谢。

Game.java 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 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 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?: 为什么不对for循环中的项目采取行动?

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. 就像板球恰当地暗示的那样:摆脱TempBricks字段,因为它所做的只是使您感到困惑。

As an aside, you will want to learn and use Java naming conventions . 顺便说一句,您将要学习和使用Java命名约定 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. 另外, Brick是单个对象,因此类应这样命名。 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. 您的问题表明您没有正确调试程序,使用IDE的调试器并逐步查看代码的运行情况,将会从中受益匪浅。 Also debug on paper -- walk through your code logically to see if it makes sense. 还要在纸上调试-逻辑上遍历代码以查看是否有意义。

You only have one TempBricks . 您只有一个 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 另外,为使removeBrick正常工作,您必须在Brick类中实现equals()hashcode()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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