我正在使用 Slick2d 创建一个游戏,其中角色“Larry”在预定义大小的地图周围推动一个“盒子”。我可以轻松地在实体上绘制图片并在实体移动时使其位置与实体一起更新。
我的主要问题是我无法找到一种方法让我的碰撞检测方法停止任何运动。当前,当检测到碰撞时,没有任何反应。当我尝试在我的运动语句中直接使用碰撞检测方法时,eclipse 说没有实体。
public abstract class Entity {
protected int x, y, w, h, dx, dy;
public Entity(int x, int y) {
this.x = x;
this.y = y;
dx = 0;
dy = 0;
w = 32;
h = 32;
}
public void render(GameContainer gc, Graphics g) throws SlickException {
}
public void update(GameContainer gc, int delta) throws SlickException {
/*x += dx;
y += dy;*/
}
public boolean isLeftCollision(Entity entity) {
if (this.x == entity.x && this.y == entity.y) {
return true;
} else {
return false;
}
}
public boolean isRightCollision(Entity entity) {
if (this.x + this.w == entity.x && this.y == entity.y) {
return true;
} else {
return false;
}
}
public boolean isTopCollision(Entity entity) {
if (this.y == entity.y && this.x == entity.x) {
return true;
} else {
return false;
}
}
public boolean isBottomCollision(Entity entity) {
if (this.y + this.h == entity.y && this.x == entity.x) {
return true;
} else {
return false;
}
}
}
public class Larry extends Entity{
Image player;
float speed = 0.2f;
public Larry(float x, float y) {
super((int) x, (int) y);
w = 32;
h = 32;
}
public void render(GameContainer gc, Graphics g) throws SlickException{
super.render(gc, g);
player = new Image("res/char/LarryUP.png");
g.drawImage(player, x, y);
g.drawString("Characters X: " + x + "\nCharacters Y: " + y, speed,
speed);
}
public void update(GameContainer gc, int delta) throws SlickException{
super.update(gc, delta);
Input input = gc.getInput();
//move right
if (input.isKeyDown(Input.KEY_RIGHT)) {
x += speed * delta;
if (x > 782) {
x -= speed * delta;
}
}
//move left
if (input.isKeyDown(Input.KEY_LEFT)) {
x -= speed * delta;
if (x < 0) {
x += speed * delta;
}
}
//move down
if (input.isKeyDown(Input.KEY_DOWN)) {
y += speed * delta;
if (y > 585) {
y -= speed * delta;
}
}
//move up
if (input.isKeyDown(Input.KEY_UP)) {
y -= speed * delta;
if (y < 0) {
y += speed * delta;
}
}
}
public class PlayState extends BasicGameState {
int stateID = 3;
Image background;
Larry larry;
Packages box;
float x = 400.0f;
float y = 300.0f;
float speed = 0.2f;
boolean quit = false;
public PlayState(int stateID) {
this.stateID = stateID;
}
@Override
public void init(GameContainer gc, StateBasedGame sbg1)
throws SlickException {
background = new Image("background.jpg");
larry = new Larry(400,300);
box = new Packages(150,300);
}
@Override
public void render(GameContainer gc, StateBasedGame sbg, Graphics g)
throws SlickException {
// TODO Auto-generated method stub
g.drawImage(background, 0, 0);
larry.render(gc, g);
box.render(gc, g);
if (quit == true) {
g.drawString("Resume (R)", 250, 100);
g.drawString("Main Menu (M)", 250, 125);
g.drawString("Quit Game (Q)", 250, 150);
if (quit == false) {
g.clear();
}
}
}
@Override
public void update(GameContainer gc, StateBasedGame sbg, int delta)
throws SlickException {
// TODO Auto-generated method stub
Input input = gc.getInput();
// escape
if (input.isKeyDown(Input.KEY_ESCAPE)) {
quit = true;
}
// when they hit escape
if (quit == true) {
if (input.isKeyDown(Input.KEY_R)) {
quit = false;
}
if (input.isKeyDown(Input.KEY_M)) {
sbg.enterState(0);
try {
Thread.sleep(250);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (input.isKeyDown(Input.KEY_Q)) {
System.exit(0);
}
}
larry.update(gc, delta);
box.update(gc, delta);
}
@Override
public int getID() {
// TODO Auto-generated method stub
return 3;
}
}
public class Packages extends Entity{
Image box;
float speed = 0.2f;
public Packages(int x, int y) {
super(x, y);
w = 32;
h = 32;
}
public void render(GameContainer gc, Graphics g) throws SlickException{
super.render(gc, g);
box = new Image("res/obj/box1.png");
g.drawImage(box, x, y);
}
public void update(GameContainer gc, int delta) throws SlickException{
super.update(gc, delta);
}
}
如果您有任何想法或可以提供任何帮助,请告诉我。我已经在网上找了几天,并与其他几个人交谈过,但似乎没有人对出了什么问题有具体的了解。