简体   繁体   English

如何使玩家撞到障碍物然后离开(Java)

[英]how to make player hit an obstacle and move away (java)

here is my code for when my player intersects a GRect, but it only works for 2 sides of the rect. 这是我的播放器与GRect相交时的代码,但仅适用于rect的两个侧面。 The other 2 give an undesirable effect. 其他2个产生不良效果。

private void gameLoop() {
    boolean done = false;
    while (!done) {

        if (pan.getBounds().intersects(b.getBounds())) {

            pan.move(-10, -10);
        }

How can i make my player "see" the rect as an obstacle and collide with it? 如何使播放器“看到”直肠障碍物并与其发生冲突? I am using key listeners to move the player. 我正在使用关键听众移动播放器。

EDIT 编辑

i added the following if statements to check for interactions of the player with each side of the obstacle, but it again seems to work for left and bottom sides. 我添加了以下if语句,以检查玩家与障碍物每一侧的相互作用,但它似乎再次适用于左侧和底部 I need all four sides . 我需要所有四个方面

I am using pan.move because it is what i use to move the player in the first place, when a key is pressed. 我使用pan.move是因为它是我首先按下按钮时用来移动播放器的功能。 So for example when i click left, i use pan.move(-10,0) and so to prevent the player from moving left i use pan.move(10,0) 因此,例如,当我单击左键时,我使用pan.move(-10,0) ,因此为了防止玩家向左移动,我使用pan.move(10,0)

    if (pan.getBounds().intersects(b1.getBounds())) {


        if ((pan.getX() +PAN_WIDTH ) > b1.getX()) {  // left
            pan.move(-10, 0);

        }
        if ((pan.getY() PAN_WIDTH ) > b1.getY()) {  //top
            pan.move(0, -10);

        }
        if ((pan.getY()) < b1.getY() + OBSTACLE_WIDTH) { // bottom
            pan.move(0, 10);

        }
        if ((pan.getX() PAN_WIDTH) < b1.getX() + OBSTACLE_HEIGHT) {  // right
            pan.move(10, 0);

        }
        }

There are lots of problem with your code, here is some corrections. 您的代码有很多问题,这里有一些更正。 You're messing up with width and height But without knowing all of your code, I don't guarantee that your way of doing it is okay. 您正在弄乱widthheight但是在不了解所有代码的情况下,我不保证您可以做到这一点。

if (pan.getBounds().intersects(b1.getBounds())) {

    if ((pan.getX() + pan.getWidth()) > b1.getX()) {  // left
        pan.move(-10, 0);
    } else if (pan.getX() < (b1.getX() + b1.getWidth())) { // right
        pan.move(10,0);
    }

    if ((pan.getY() + pan.getHeight()) > b1.getY()) {  //top
        pan.move(0, -10);
    } else if (pan.getY() < (b1.getY() + b1.getHeight())) { // bottom
        pan.move(0, 10);
    }
}

Also, I suggest you to change your constant to methods like getWidth() . 另外,建议您将常量更改为类似getWidth()方法。 In the case you change the dimensions of your obstacle or player, you'll not have to change the constants too. 如果您更改障碍物或播放器的尺寸,则也不必更改常数。

Also, moving your player is not a good way of doing it. 另外,移动播放器也不是一个好方法。 Your player should have two fields representing the moves you'll perform. 您的玩家应该有两个代表您将执行的动作的字段。 Then in the case of collision, you just have to put these fields to 0. You don't want your player to move when collision, you just want it to stop going more left/right/up/down. 然后在发生碰撞的情况下,您只需要将这些字段设置为0。您不希望玩家在碰撞时移动,而只是希望它停止向左/向右/向上/向下移动。

For example if you change the speed of your player, then you'll have to change all of these pan() manually. 例如,如果您更改播放器的速度,则必须手动更改所有这些pan() That's going to get you problems if you don't realize that. 如果您不意识到这一点,将会给您带来麻烦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM