Java 方块推挤游戏麻烦与滑头2D

Java 方块推挤游戏麻烦与滑头2D,java,collision-detection,slick2d,Java,Collision Detection,Slick2d,我正在使用Slick2d创建一个游戏,其中一个角色“拉里”在预定义大小的地图周围推一个“盒子”。我在一个实体上画一张图片,并在实体移动时使其位置随实体更新,这一点没有问题 我的主要问题是,我找不到一种方法,我的碰撞检测方法,以停止任何运动。当前,当检测到碰撞时,不会发生任何事情。当我试图在我的运动语句中直接使用碰撞检测方法时,eclipse说没有实体 } } 如果你有任何想法或可以提供任何帮助,请让我知道。我已经在网上找了好几天了,还和其他几个人谈过,但似乎没有人知道到底是什么错了 您不

我正在使用Slick2d创建一个游戏,其中一个角色“拉里”在预定义大小的地图周围推一个“盒子”。我在一个实体上画一张图片,并在实体移动时使其位置随实体更新,这一点没有问题

我的主要问题是,我找不到一种方法,我的碰撞检测方法,以停止任何运动。当前,当检测到碰撞时,不会发生任何事情。当我试图在我的运动语句中直接使用碰撞检测方法时,eclipse说没有实体


}


}




如果你有任何想法或可以提供任何帮助,请让我知道。我已经在网上找了好几天了,还和其他几个人谈过,但似乎没有人知道到底是什么错了

您不使用碰撞方法, 在类Larry的更新方法中,设置一个条件,如果发生碰撞,不要麻烦移动玩家

(例如,当您试图向下移动时

if (input.isKeyDown(Input.KEY_UP)) {
        if(isBottomCollision(this)) {
             y -= speed * delta;
             if (y < 0) {
                 y += speed * delta;
             }
        }
    }

如果您这样做: this.x==entity.x&&this.y==entity.y 它只需要图像/碰撞的1点,而不是整个左侧

你要做的是在它周围画一个形状(矩形、圆环、多边形等) 然后像这样使用intersects()方法:

Rectangle entityCollision;
Rectangle boxCollision;

public Entity() {
    //your code here
}

public void init(arguments){
    entityCollision = new Rectangle(arguments);
    boxCollision = new Rectangle(arguments);
}

public void render(arguments){
    //your code here
}

public void update(arguments){

    if(entityCollision.intersects(boxCollision)){
        //this is where the computer checks the collision
        //your code here
    }

}
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);
}
}
if (input.isKeyDown(Input.KEY_UP)) {
        if(isBottomCollision(this)) {
             y -= speed * delta;
             if (y < 0) {
                 y += speed * delta;
             }
        }
    }
Direction {LEFT, RIGHT, UP,DOWN} //global
public boolean isCollision(Entity entity, direction direction)
public boolean isCollision(Entity entity,int x, int y)
Rectangle entityCollision;
Rectangle boxCollision;

public Entity() {
    //your code here
}

public void init(arguments){
    entityCollision = new Rectangle(arguments);
    boxCollision = new Rectangle(arguments);
}

public void render(arguments){
    //your code here
}

public void update(arguments){

    if(entityCollision.intersects(boxCollision)){
        //this is where the computer checks the collision
        //your code here
    }

}