我正在尝试制作一个java沙子游戏而且不能超过一点。我已经制作了我的方法,在mouseX和mouseY上绘制一个矩形,我已经设置了它,所以它更新每一帧,所以它不断跟随鼠标。
我假设是我会使用数组来创建每个矩形,并从那里使用预定义的算法浮动到地面,我都很好,我只是不明白如何'将我的方法'挂钩到一个数组。
这是我用来绘制矩形的方法(在它自己的类中称为方法)
import org.newdawn.slick.Graphics;
public class Methods {
public Graphics g = new Graphics();
public int sizeX = 4;
public int sizeY = 4;
public void drawParticle(float x, float y){
g.drawRect(x, y, sizeX, sizeY);
}
}
这是我的主要课程
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();
int mouseX;
int mouseY;
public void init(GameContainer container, StateBasedGame game) throws SlickException{
}
public void render(GameContainer container, StateBasedGame game, Graphics g) throws SlickException {
m.drawParticle(mouseX, mouseY);
}
public void update(GameContainer container, StateBasedGame game, int delta) {
}
public void mouseReleased(int button, int x, int y){
mouseX = 0;
mouseY = 0;
}
public void mouseDragged(int oldx, int oldy, int newx, int newy) {
mouseX = newx;
mouseY = newy;
}
public int getID() {
return ID;
}
}
但是当我点击时,鼠标后面只有一个矩形,而不是鼠标创建的多个矩形:L
答案 0 :(得分:0)
公共变量:
Rectangle boxes[] = new Rectangle[maxnum];
int boxnum = 0;
鼠标移动:
boxes[boxnum] = new Rectangle[e.getX(), e.getY(), sizeX, sizeY);
boxnum = boxnum + 1;
绘制粒子时:
counter = 0;
do
{
g.drawRect(boxes[counter].x, boxes[counter].y, sizeX, sizeY);
counter = counter + 1;
} while (counter < maxnum);
其中maxnum是您将拥有的最大盒子数。这样,您可以在阵列中存储多个矩形,并在更新屏幕时浏览阵列并绘制它们。希望这会有所帮助。