0

我正在使用 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);
}
}

如果您有任何想法或可以提供任何帮助,请告诉我。我已经在网上找了几天,并与其他几个人交谈过,但似乎没有人对出了什么问题有具体的了解。

4

2 回答 2

0

You don't use your Collision Method, In your update method of the class Larry, put a condition, if there is a collision don't bother move your player

(example when you're trying to move down

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

like this, if the player try to go down but there is a collision, he will not be able to move. You can make only one method to get collision with an extra arguments (an Enum) or two (x and y) which told the method which way the player is trying to go

Direction {LEFT, RIGHT, UP,DOWN} //global
public boolean isCollision(Entity entity, direction direction)

or

public boolean isCollision(Entity entity,int x, int y)
于 2014-02-17T11:26:41.373 回答
0

如果你这样做: 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
    }

}
于 2014-06-02T17:03:03.733 回答