0

我有以下代码:

if(input.isKeyDown(Input.KEY_RIGHT)){
  player = movingRIGHT;
  pposX -= 0.15f;
  if(playerR.intersects(rec1)){
    pposX += 0.15f;
  }
}

if(input.isKeyDown(Input.KEY_LEFT)){
  player = movingLEFT;
  pposX += 0.15f;
  if(playerR.intersects(rec1)){
    pposX -= 0.15f;
}

还有两个玩家 - playerR 和 rec1:

playerR = new Rectangle(shiftX-174,shiftY+200,player.getWidth(),player.getHeight());
rec1 = new Rectangle(448+pposX, 640-128+pposY, 32, 32);

我的球员撞墙后没有停下来。我想知道坐标是否足够好,所以我尝试在完全相同的坐标处绘制矩形没有问题。但是,我的播放器刚刚通过。

我正在使用带有此代码的 java slick2d:

package javagame;

import org.lwjgl.input.Mouse;
import org.newdawn.slick.Animation;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

public class Play extends BasicGameState {

Animation player,movingUP,movingDOWN,movingLEFT,movingRIGHT;
Image worldMap;
Image cat;
boolean quit = false;
int [] duration = {200,200,200,200};
int [] duration2 = {200,200};
float pposX;
float pposY;
float shiftX = pposX + 430;
float shiftY = pposY + 320;
float height;
int x = 0;
int y = 0;
boolean jumping = false;
float verticalSpeed;
float mapPosX = 0;
float mapPosY = 0;
Rectangle rec1;
Rectangle rec2;
Rectangle rec3;
Rectangle rec4;
Rectangle rec5;
Rectangle rec6;
Rectangle rec7;
Rectangle rec8;
Rectangle playerR;
boolean Win;
String a = "Victory!";
public Play (int state){

}

public void init(GameContainer gc,StateBasedGame sbg) throws SlickException{
    worldMap = new Image("res/map.png");
    Image[] walkUp = {new Image("res/buckysBack.png") , new Image("res/buckysBack.png")};
    Image[] walkLeft = {new Image("res/1L.png") , new Image("res/2L.png"), new Image("res/3L.png"), new Image("res/4L.png")};
    Image[] walkRight = {new Image("res/1R.png") , new Image("res/2R.png"), new Image("res/3R.png"), new Image("res/4R.png")};
    Image[] walkDown = {new Image("res/buckysFront.png") , new Image("res/BuckysFront.png")};


    movingLEFT = new Animation(walkLeft , duration , true);
    movingRIGHT = new Animation(walkRight,duration,true);
    movingUP = new Animation(walkUp,duration2,false);
    movingDOWN = new Animation(walkDown,duration2,false);

    rec1 = new Rectangle(448+pposX, 640-128+pposY, 32, 32);
    rec2 = new Rectangle(480,640-160,32,32);
    rec3 = new Rectangle(512,640-192,64,32);


    player = movingRIGHT;
}

public void render(GameContainer gc,StateBasedGame sbg,Graphics g)throws SlickException{
    worldMap.draw(pposX,pposY);
    player.draw(shiftX-174,shiftY+200);
    g.drawRect(448+pposX, 640-128+pposY, 32, 32);
    g.drawRect(shiftX-174,shiftY+200,player.getWidth(),player.getHeight());
    g.setColor(Color.blue);
    g.drawString("Your position x: " + pposX * -1 + "\n              y: " + pposY  , 600, 30);
    int mx = Mouse.getX();
    int my = Mouse.getY();
    g.drawString("Your mouse x: " + mx + "y: " + my, 400,400);
    playerR = new Rectangle(shiftX-174,shiftY+200,player.getWidth(),player.getHeight());


    if(quit == true){
        g.drawString("Resume(R)", sbg.getContainer().getWidth()/2, 100);
        g.drawString("Main Menu(M)", sbg.getContainer().getWidth()/2, 150);
        g.drawString("Quit(Q)", sbg.getContainer().getWidth()/2, 200);
        if(quit==false){
            g.clear();
        }
    }
    if(Win){
        g.setColor(Color.red);
        g.drawString(a,sbg.getContainer().getWidth()/2 - a.length()/2,300);
    }


}

public void update(GameContainer gc,StateBasedGame sbg,int delta)throws SlickException{
    Input input = gc.getInput();

    if(input.isKeyDown(Input.KEY_UP)){
        pposY += 0.15f;
    }
    if(input.isKeyDown(Input.KEY_DOWN)){
        pposY -= 0.15f;
    }

    if(jumping){
        verticalSpeed += .007f * delta;
    }
    pposY += verticalSpeed;



    if(input.isKeyDown(Input.KEY_RIGHT)){
        player = movingRIGHT;
        pposX -= 0.15f;
        if(playerR.intersects(rec1)){
        pposX += 0.15f;
        }
        if(pposX>3100){
            Win = true;
        }
    }
    if(input.isKeyDown(Input.KEY_LEFT)){
        player = movingLEFT;
        pposX += 0.15f;
        if(playerR.intersects(rec1)){
        pposX -= 0.15f;
        }
        if(pposX> -1.5f){
            pposX-= 0.15f;
        }
    }

}
public int getID(){
    return 1;
}
}

如何让玩家停在墙上?

4

2 回答 2

2
playerR = new Rectangle(shiftX-174,shiftY+200,player.getWidth(),player.getHeight());

首先,我不知道是什么shiftX。您必须每帧更新矩形。例子:

playerR = new Rectangle(pposX, pposY, player.getWidth(), player.getHeight());

现在将该代码放在碰撞检测之前。请注意,您应该将宽度和高度存储在一个变量中,以使其更快,而不必调用getWidth每一getHeight帧。

于 2013-01-03T12:14:40.577 回答
0

冲突的一个非常常见的问题是更新的离散性。由于您更新了位置,它基本上会传送,并且可以在它应该碰撞的对象上这样做。因此,请检查台阶与该区域和其他物体(或墙壁)的面积相比是否足够小。

于 2013-02-19T00:13:10.290 回答