0

好的,所以每当我单击鼠标时,我的图像都会沿着 y 轴直接向下移动,我唯一的问题是我不知道如何让它在它到达屏幕底部时停止,有人可以帮忙吗?

    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;
    String Build = "1.1";

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

    public void render(GameContainer container, StateBasedGame game, Graphics g) throws SlickException {
        for (Point p : point) {
            if (p != null) {
                m.drawParticle(p.x, p.y += 1);
            }
        }
        g.drawString("Particle Test", 680, 0);
        g.drawString("Build: " + Build, 680, 15);
        g.drawString("Pixels: " + num, 10, 25);
    }

    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] = new Point(pressedX, pressedY);
        }

    public int getID() {
        return ID;
    }

}
4

1 回答 1

0

我想在某个地方你会想要在渲染粒子之前检查粒子的 x/y 位置,并在它超出范围时将其从数组中删除......

public void render(GameContainer container, StateBasedGame game, Graphics g) throws SlickException {
    for (int index = 0; index < point.length; index++) {
        Point p = point[index];
        if (p != null) {
            p.y++;
            if (p.y > height) { // You'll need to define height...
                point[index] = null; // Or do something else with it??
            } else {
                m.drawParticle(p.x, p.y);
            }
        }
    }
    g.drawString("Particle Test", 680, 0);
    g.drawString("Build: " + Build, 680, 15);
    g.drawString("Pixels: " + num, 10, 25);
}

你也可以做一个先发制人的检查,这样你就可以知道屏幕底部有哪些点......

        if (p != null) {
            if (p.y >= height) { // You'll need to define height...
                // Do something here
            } else {
                p.y++;
                m.drawParticle(p.x, p.y);
            }
        }
于 2012-10-08T04:57:10.683 回答