-3

我编写了一个简单的 Java 游戏,其中屏幕上有两个矩形,一个矩形应该移动,另一个应该保持静止,移动的矩形随着键盘箭头输入移动,可以向上、向下、向左或向右移动。我遇到的问题是,当只有一个矩形应该(rectOne)并且rectTwo应该保持静止时,两个矩形都在移动,我的变量设置如下所示:

    float buckyPositionX = 0;
    float buckyPositionY = 0;
    float shiftX = buckyPositionX + 320;//keeps user in the middle of the screem
    float shiftY = buckyPositionY + 160;//the numbers are half of the screen size
//my two rectangles are shown bellow 
java.awt.geom.Rectangle2D.Float rectOne = new Rectangle2D.Float(shiftX, shiftY,90,90);
java.awt.geom.Rectangle2D.Float rectTwo = new Rectangle2D.Float(500 + buckyPositionX, 330 + buckyPositionY, 210, 150);

坐标变量 buckyPositionX 和 buckyPositionY 为 0,0,因此它们显示我屏幕的左两个角,坐标变量 shiftX 和 shiftY 显示屏幕中心的坐标,以便移动矩形始终居中。

包含所有游戏代码的 Play 类由几个方法组成,重要的是更新和渲染,更新处理矩形移动速度和键盘输入,以向您展示内部内容的示例更新类,这里是当他们按下键时的代码示例:

if(input.isKeyDown(Input.KEY_UP)){buckyPositionY += 2;}

这适用于所有键盘箭头输入。接下来是 render 方法,它处理在屏幕和图形上绘制所有内容,该方法包括在屏幕上绘制先前使用变量编码的矩形。

因此,当我尝试使用键盘输入移动矩形时,我在屏幕上绘制了两个矩形,这两个矩形同时一起移动,我认为我如何设置矩形坐标并尝试播放存在问题例如,从它们中删除 +buckyPositionX 和 +buckyPositionY 但会出现同样的问题,如果您对可以解决此问题的方法有任何想法,请告诉我。

先感谢您。

编辑1:

这是我要求的游戏课的完整代码:

    imports are here

    public class Play extends BasicGameState{

    Animation bucky, movingUp, movingDown, movingLeft, movingRight;
    Image worldMap;
    int[] duration = {200, 200};//how long frame stays up for
    float buckyPositionX = 0;
    float buckyPositionY = 0;
    float shiftX = buckyPositionX + 320;//keeps user in the middle of the screem
    float shiftY = buckyPositionY + 160;//the numbers are half of the screen size

    java.awt.geom.Rectangle2D.Float rectOne = new Rectangle2D.Float(shiftX, shiftY,90,90);
    java.awt.geom.Rectangle2D.Float rectTwo = new Rectangle2D.Float(500 + buckyPositionX, 330 + buckyPositionY, 210, 150);

    public Play(int state){
    }   
    public void init(GameContainer gc, StateBasedGame sbg) throws SlickException{
          worldMap = new Image("res/world.png");
          Image[] walkUp = {new Image("res/b.png"), new Image("res/b.png")}; //these are the images to be used in the "walkUp" animation
          Image[] walkDown = {new Image("res/f.png"), new Image("res/f.png")};
          Image[] walkLeft = {new Image("res/l.png"), new Image("res/l.png")};
          Image[] walkRight = {new Image("res/r.png"), new Image("res/r.png")};

    movingUp = new Animation(walkUp, duration, false);
    movingDown = new Animation(walkDown, duration, false);  
    movingLeft = new Animation(walkLeft, duration, false);  
    movingRight = new Animation(walkRight, duration, false);
    bucky = movingDown;//facing screen initially on startup
    }


    public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{
    worldMap.draw(buckyPositionX, buckyPositionY);//position 0,0
    bucky.draw(shiftX, shiftY);//makes him appear at center of map

    g.fillRect((float)rectOne.getX(), (float)rectOne.getY(), (float)rectOne.getWidth(), (float)rectOne.getHeight());
    g.fillRect((float)rectTwo.getX(), (float)rectTwo.getY(), (float)rectTwo.getWidth(), (float)rectTwo.getHeight());
}

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

    //up
    if(input.isKeyDown(Input.KEY_UP)){
        bucky = movingUp;//changes the image to his back
        buckyPositionY += 2;;//increase the Y coordinates of bucky (move him up)
        if(buckyPositionY>162){//if I reach the top 
            buckyPositionY -= 2;//stops any further movement in that direction
        }
    }

    //down
    if(input.isKeyDown(Input.KEY_DOWN)){
        bucky = movingDown;
        buckyPositionY -= 2;
        if(buckyPositionY<-550){
            buckyPositionY += 2;//basically change the direction if + make -
    }}
    //left
    if(input.isKeyDown(Input.KEY_LEFT)){
        bucky = movingLeft;
        buckyPositionX += 2;
        if(buckyPositionX>324){
            buckyPositionX -= 2;//delta * .1f
    }}
    //right
    if(input.isKeyDown(Input.KEY_RIGHT)){
        bucky = movingRight;
        buckyPositionX -= 2;
        if(buckyPositionX<-776){
            buckyPositionX += 2;
    }}}   


    public int getID(){
        return 1;
    }}

回答问题:

我在我的 rectTwo 变量中使用变量 buckyPositionX 和 buckyPositionY 的原因是,当我尝试使用 g.fillRect 函数绘制它时,这些输入一切正常,rectOne 移动并且 RectTwo 保持静止,但是当我从通过说 g.fillRect(rectOne.X,rectOne.Y...) 我得到了这个问题,两个矩形一起移动,这很奇怪,因为坐标和大小与使用 g 绘制时完全相同。 fillRect(500 + buckyPositionX, 330 + buckyPositionY, 210, 150) 与 rectOne 相同。我将尝试使用一些代码使其更清晰:

如果我在我的变量中使用它:

java.awt.geom.Rectangle2D.Float rectOne = new Rectangle2D.Float(shiftX, shiftY,90,90);
        java.awt.geom.Rectangle2D.Float rectTwo = new Rectangle2D.Float(500 + buckyPositionX, 330 + buckyPositionY, 210, 150);

这在我的渲染方法中:

 g.fillRect((float)rectOne.getX(), (float)rectOne.getY(), (float)rectOne.getWidth(), (float)rectOne.getHeight());
        g.fillRect((float)rectTwo.getX(), (float)rectTwo.getY(), (float)rectTwo.getWidth(), (float)rectTwo.getHeight());

rectOne 和 rectTwo 这两个矩形一起移动。

但是如果我在我的渲染方法中使用它而不设置任何变量:

g.fillRect(shiftX, shiftY,90,90)
g.fillRect(500 + buckyPositionX, 330 + buckyPositionY, 210, 150)

尽管我的碰撞不适用于这种方法(这就是我要修复它的原因),但一切都可以视觉上工作,矩形都出现在屏幕上并且 rectOne 移动而 rectTwo 保持静止。

我将发表一篇新文章更详细地解释我的问题,因为这个问题已经开始变得过于拥挤和偏离主题。

4

2 回答 2

1

这是一个循环吗?

float buckyPositionX = 0;
float buckyPositionY = 0;
float **shiftX** = buckyPositionX + 320;//keeps user in the middle of the screem
float **shiftY** = buckyPositionY + 160;//the numbers are half of the screen size
//my two rectangles are shown bellow 
java.awt.geom.Rectangle2D.Float rectOne = new Rectangle2D.Float(shiftX, shiftY,90,90);
*java.awt.geom.Rectangle2D.Float rectTwo = new Rectangle2D.Float(500 + buckyPositionX, 330 + buckyPositionY, 210, 150);*
if(input.isKeyDown(Input.KEY_UP)){**buckyPositionY** += 2;}

如果 rectTwo 保持不动,为什么要使用 buckyPositiony?- 你正在用 keyup 更新它

于 2013-01-04T00:20:44.473 回答
1

我想这个问题与有关?

无论如何,但是 rectangles 依赖于buckyPositionY所以通过更新这个变量,你实际上是在更新两个矩形。

试试这样的东西。最好您只需将坐标更新为rectOne但据我所知, Rectangle2D没有setX方法

if (input.isKeyDown(Input.KEY_UP)){
  rectOne = new Rectangle2D.Float((float)rectOne.getX(), (float)rectOne.getY()+2, (float)90, (float)90);
}
于 2013-01-04T00:32:49.220 回答