0

所以我整天都在努力让我的盒子保存它们在数组中的位置,最后我想我想出了一些东西(在你们的帮助下),但它就是不工作......有人可以请告诉我为什么?

控制类:

import java.awt.Point;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

public class Control extends BasicGameState {
    public static final int ID = 1;

    public Methods m = new Methods();
    public Point[] point = new Point[(800 * 600)];

    int pressedX;
    int pressedY;
    int num = 0;

    public void init(GameContainer container, StateBasedGame game) throws SlickException{
    }

    public void render(GameContainer container, StateBasedGame game, Graphics g) throws SlickException {
        m.drawParticle(pressedX, pressedY);
    }

    public void update(GameContainer container, StateBasedGame game, int delta) {
    }

    public void mousePressed(int button, int x, int y) {
        pressedX = x;
        pressedY = y;
        num = num + 1;
        point[num].x = pressedX;
        point[num].y = pressedY;
        }

    public int getID() {
        return ID;
    }


}

方法类:

    import org.newdawn.slick.Graphics;

public class Methods {

    public Graphics g = new Graphics();

    public int sizeX = 1;
    public int sizeY = 1;

    public void drawParticle(float x, float y){
        g.drawRect(x, y, sizeX, sizeY);
    }

}
4

1 回答 1

0

虽然您已经初始化了点数组的大小,但您还没有初始化内容。

public void mousePressed(int button, int x, int y) {
    pressedX = x;
    pressedY = y;
    num++;
    point[num] = new Point(pressedX, pressedY);
}

还认为在您的render方法中,您需要重新渲染图形(我可能弄错了,我以前没有使用过 Slick2D)...

有了类似...

public void render(GameContainer container, StateBasedGame game, Graphics g) throws SlickException {
    for (Point p : point) {
        if (p != null) {
            m.drawParticle(p.x, p.y);
        }
    }
    m.drawParticle(pressedX, pressedY);
}

我也很好奇你创建你自己的Graphics,特别是当render方法通过你时,你可能想进一步检查并确保这是可以接受的......

于 2012-10-08T04:04:20.313 回答