停止图像移动

时间:2012-10-08 04:50:28

标签: java image slick2d particles

好吧,所以每当我点击鼠标时,我的图像沿着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;
    }

}

1 个答案:

答案 0 :(得分: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);
            }
        }