slick2d - 无法使鼠标单击事件正常工作

slick2d - can't get mouse click event to work correctly

哟!我正在尝试使用 Slick2d、JBox2d 和 Tiled 在 Java 中制作一个小型爱好游戏,但我遇到了一个烦人的问题。我的游戏目前包括一艘在小舞台上飞来飞去的宇宙飞船。我已经设法使移动恰到好处,甚至还附上了一个可以旋转以跟随光标的小炮。相机对焦,使船始终居中。我的问题是我无法获得任何方法来告诉我鼠标按钮是否已被按下以工作。当船静止时它们工作得很好,但当它加速时它停止工作。当船达到最大速度时它再次工作!所以当船加速时鼠标按钮事件不会触发!?如果您对可能发生的事情有所了解,请帮帮我 :D :D

ship is centered, cannon follows mouse. when ship is accelerating mouse press events are not working?? why?

游戏状态:

package code;

import java.util.ArrayList;

import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.World;
import org.lwjgl.input.Mouse;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.tiled.TiledMap;

public class Play extends BasicGameState {
    public final int ID;
    public final int PPM = 20;

    public World world;
    public Player player;
    public ArrayList<BasicObject> pool = new ArrayList<BasicObject>();
    public TiledMap map;
    public Camera camera;

    public Play(int ID) {
        this.ID = ID;
    }

    @Override
    public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
        world = new World(new Vec2(0, 0));
        map = new TiledMap("res/map0.tmx");
        camera = new Camera(0, 0, gc.getWidth(), gc.getHeight());
        gc.getGraphics().setBackground(Color.white);

        spawn();
    }

    @Override
    public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
        g.translate(camera.x, camera.y);
        map.render(0, 0);
        for (BasicObject bo : pool) {
            bo.draw(g, PPM);
        }

        player.setAlpha((float) Math.toDegrees(Math.atan2(
                (((gc.getHeight() - Mouse.getY()) - camera.y) - player.getCenterY()*PPM),
                ((Mouse.getX() - camera.x) - player.getCenterX()*PPM)
                )));

        player.draw(g, PPM);

        //g.drawLine(player.getCenterX()*PPM, player.getCenterY()*PPM, Mouse.getX()-(camera.x), (gc.getHeight() - Mouse.getY())-(camera.y));
    }

    @Override
    public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException { 
        Input input = gc.getInput();
        player.update(delta);
        for (BasicObject bo : pool) {
            bo.update(delta);
        }

        if (input.isKeyDown(input.KEY_A)) {player.move("left", delta);}
        if (input.isKeyDown(input.KEY_D)) {player.move("right", delta);}
        if (input.isKeyDown(input.KEY_W)) {player.move("up", delta);}
        if (input.isKeyDown(input.KEY_S)) {player.move("down", delta);}
        if (Mouse.isButtonDown(0)) {
            System.out.println("mousepressed");
        }


        //player.getBody().applyForceToCenter(new Vec2((Mouse.getX()-(camera.x))/PPM, ((gc.getHeight() - Mouse.getY())-(camera.y))/PPM).sub(player.getBody().getPosition()));

        float deltaF = (float) delta/1000;
        world.step(deltaF, 6, 2);

        camera.x = player.getBody().getPosition().mul(PPM).mul(-1).x + gc.getWidth()/2;
        camera.y = player.getBody().getPosition().mul(PPM).mul(-1).y + gc.getHeight()/2;
    }

    public void spawn() {
        for(int i=0;i<map.getObjectCount(0);i++) {
            float theX = map.getObjectX(0, i);
            float theY = map.getObjectY(0, i);
            float theWidth = map.getObjectWidth(0, i);
            float theHeight = map.getObjectHeight(0, i);

            addWall(theX, theY, theWidth, theHeight);
        }

        for(int i=0;i<map.getObjectCount(1);i++) {
            if (map.getObjectType(1, i).equals("Player")) {
                float theX = map.getObjectX(1, i);
                float theY = map.getObjectY(1, i);
                float theWidth = 40;
                float theHeight = 60;

                player = new Player((theX + theWidth/2)/PPM, (theY + theHeight)/PPM, theWidth/PPM, theHeight/PPM, world);
            }
        }
    }

    public void addWall(float x, float y, float width, float height) {
        pool.add(new Wall((x + width/2)/PPM, (y + height/2)/PPM, width/PPM, height/PPM, world));
    }

    @Override
    public int getID() {
        // TODO Auto-generated method stub
        return ID;
    }
}

问题来了。 Mouse.isButtonDown(0) 在飞船加速时不起作用。我通过在船移动时按下按钮并检查控制台的输出来测试它。

public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException { 
    Input input = gc.getInput();
    player.update(delta);
    for (BasicObject bo : pool) {
        bo.update(delta);
    }

    if (input.isKeyDown(input.KEY_A)) {player.move("left", delta);}
    if (input.isKeyDown(input.KEY_D)) {player.move("right", delta);}
    if (input.isKeyDown(input.KEY_W)) {player.move("up", delta);}
    if (input.isKeyDown(input.KEY_S)) {player.move("down", delta);}
    if (Mouse.isButtonDown(0)) {
        System.out.println("mousepressed");
    }


    //player.getBody().applyForceToCenter(new Vec2((Mouse.getX()-(camera.x))/PPM, ((gc.getHeight() - Mouse.getY())-(camera.y))/PPM).sub(player.getBody().getPosition()));

    float deltaF = (float) delta/1000;
    world.step(deltaF, 6, 2);

    camera.x = player.getBody().getPosition().mul(PPM).mul(-1).x + gc.getWidth()/2;
    camera.y = player.getBody().getPosition().mul(PPM).mul(-1).y + gc.getHeight()/2;
}

如果您需要查看来自更多 类 的代码,请告诉我,因为我不知道问题出在哪里,因此不知道 post 的代码是什么:p

LWJGLclass和Slick的对应关系不太了解。无论如何,我认为你可以使用 MouseListener 给你的东西。您可以覆盖 mouseClicked(int button, int x, int y, int clickCount) 方法或 mousePressed

@Override
public void mouseClicked(int button, int x, int y, int clickCount) {
    if(button == Input.MOUSE_LEFT_BUTTON){
        System.out.println("LeftMouseClicked");
    }else{
        System.out.println("Else button clicked");}
}

这是由 BasicGameState 继承自 MouseListener 的事实提供的。

这绝对有效。然后你可以设置标志来知道什么时候从update方法

点击它

好的。我的问题的答案有点尴尬.. :p 结果是我的笔记本电脑出了问题。出于某种原因,在键入 xD 时无法使用鼠标垫上的按钮,我想这是某种安全功能,因此您在尝试键入时不要用手掌随机点击东西:)

插入usb鼠标,程序运行良好!非常感谢 RPresle 的帮助 ;)