哟!我正在尝试使用Slick2d,JBox2d和Tiled在Java中制作一个小型的爱好游戏,但我遇到了一个恼人的问题。我的游戏目前包括一个在小舞台上飞来飞去的宇宙飞船。我设法让运动恰到好处,甚至还有一个旋转的小型加农炮跟随光标。相机是聚焦的,所以船总是在中心。我的问题是我无法获得任何方法告诉我是否按下鼠标按钮才能工作。当船静止不动时它们工作正常,但是当它加速时它会停止工作..当船达到最大速度时它会再次起作用!所以当船加速时,鼠标按钮事件不会被触发!?如果您对可能发生的事情有所了解,请帮助我: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
答案 0 :(得分:1)
确定。我的问题的答案有点令人尴尬..:p原来是我的笔记本电脑是问题所在。由于某种原因,在键入xD时无法使用鼠标垫上的按钮我猜它具有某种安全功能,因此您在尝试输入时不要用手掌点击随机内容:)
插入USB鼠标,程序运行良好!非常感谢RPresle试图提供帮助;)
答案 1 :(得分:0)
我不太了解LWJGL类和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");}
}
这是BasicGameStat
e继承自MouseListener
的事实。
这绝对有效。然后,您可以设置标志以了解何时从update
方法