所以我一直在努力让我的盒子整天保存在一个阵列中的位置,最后我想我想出了一些东西(在你们的帮助下得到很多帮助)而且它只是不起作用......有谁能告诉我为什么?
控制类:
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);
}
}
答案 0 :(得分: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
方法通过你时,你可能想要进一步检查,并确保这是可以接受的。 ..