哟!我正在尝试使用 Slick2d、JBox2d 和 Tiled 在 Java 中制作一个小型爱好游戏,但我遇到了一个恼人的问题。我的游戏目前由一艘在小舞台中飞行的宇宙飞船组成。我设法让 Action 恰到好处,甚至附加了一门可以旋转以跟随光标的小加农炮。相机已聚焦,以便船始终处于中心位置。我的问题是我无法获得任何方法来告诉我鼠标按钮是否已按下来工作。当船静止时它们工作得很好,但是当它加速时它就停止工作。当船达到最大速度时它又工作了!所以当船加速时鼠标按钮事件不会触发!?如果您对可能发生的情况有所了解,请帮助我:D:D
游戏状态:
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;
}
如果您需要查看更多类的代码,请告诉我,因为我不知道问题出在哪里,因此不知道要发布哪些代码:p
最佳答案
好的。我的问题的答案有点尴尬..:p 原来是我的笔记本电脑出了问题。由于某种原因,在输入 xD 时无法使用鼠标垫上的按钮,我想这是某种安全功能,因此您在尝试输入时不会用手掌点击随机内容:)
插入 USB 鼠标,程序运行得很好!非常感谢 RPresle 的帮助;)
关于java - slick2d - 无法让鼠标单击事件正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33067390/