【问题标题】:Stopping image movement停止图像移动
【发布时间】:2012-10-08 04:50:27
【问题描述】:

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

}

【问题讨论】:

    标签: java image slick2d particles


    【解决方案1】:

    我想在某个地方你会想在渲染粒子之前检查它的 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);
                }
            }
    

    【讨论】:

    • 谢谢,现在你知道如何让它停止向下移动吗?
    • 在第二个示例中,如果 p.y 等于或大于可用高度,则它的 y 位置不应再次更新...显然,如果您将其从数组中删除(设置数组position 为 null),它不仅不会再移动,也不会被绘制 ;)
    • 但我可以让它保持静止吗?
    • 是的。一旦 p.y >= 可用高度,第二个样本将停止更新 y 位置...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-18
    • 1970-01-01
    • 1970-01-01
    • 2014-02-22
    相关资源
    最近更新 更多