Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/382.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 2D游戏,我的两个矩形都在移动,而只有一个应该移动_Java_Rectangles_2d Games - Fatal编程技术网

Java 2D游戏,我的两个矩形都在移动,而只有一个应该移动

Java 2D游戏,我的两个矩形都在移动,而只有一个应该移动,java,rectangles,2d-games,Java,Rectangles,2d Games,我编写了一个简单的Java游戏,屏幕上有两个矩形,其中一个应该移动,另一个应该保持静止,移动的矩形随着键盘箭头输入移动,可以上下左右移动。我遇到的问题是,两个矩形都在移动,而其中只有一个矩形应该保持静止,而矩形二应该保持静止,我的变量设置如下所示: float buckyPositionX = 0; float buckyPositionY = 0; float shiftX = buckyPositionX + 320;//keeps user in the middl

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

    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类由几个方法组成,重要的是update和render,更新处理矩形移动速度和键盘输入,以向您展示更新类中的内容示例,以下是按下up键时的代码示例:

if(input.isKeyDown(Input.KEY_UP)){buckyPositionY += 2;}
这是为所有键盘箭头输入完成的。接下来是渲染方法,该方法处理在屏幕上绘制所有内容和图形,该方法包括在屏幕上绘制先前使用变量编码的矩形

当我用键盘输入移动矩形时,屏幕上画了两个矩形,两个矩形同时移动,我认为我如何设置矩形坐标存在问题,并尝试过使用它们,例如从它们中删除+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;
    }}
在我的渲染方法中:

 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());
两个矩形,矩形一和矩形二一起移动

但如果我在渲染方法中使用此选项,而没有任何变量集:

g.fillRect(shiftX, shiftY,90,90)
g.fillRect(500 + buckyPositionX, 330 + buckyPositionY, 210, 150)
虽然我的碰撞不适用于这种方法,但一切都可以在视觉上工作,这就是为什么我要修复它。矩形都出现在屏幕上,矩形一移动,而矩形二保持静止

我会写一篇新帖子,更详细地解释我的问题,因为这篇帖子已经开始变得过于拥挤和离题了。

这是一个循环吗

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;}

如果Rection Two保持静止,为什么要使用buckyPositiony你正在用keyup更新这个问题,我想这个问题与什么有关

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

试试这样吧。最好你只需要将坐标更新为rectOne,但从我所看到的来看,没有setX方法可以更新为Rectangle2D


我们需要更多的代码。在哪里更新矩形?这是创建和发布矩形的完美案例。否则,我们将被迫使用魔法或猜测未显示的代码,以帮助您解决此问题。某些内容在不应该是静态的情况下是静态的。该代码已根据要求添加到我的帖子中,很抱歉在我之前的帖子中缺少详细信息。在多次重新阅读该问题后,发现它无法理解,我对你的答复的意见上升了+1我很抱歉在我的问题中缺少细节,现在已经用我的完整类代码更新了。我编辑了我的帖子来回答你关于rectTwo中包含buckyPositionX和buckyPositionY变量的问题。感谢你回答我的问题,我很抱歉在我之前的帖子中缺少细节,我的完整游戏类的代码现在已经添加。您编写的if语句移动了矩形,但将矩形下的图像保留在同一位置,这是我的错,因为缺少细节,您不知道矩形下甚至有一个播放器,因此对此我感到抱歉。你能帮我让shiftX的玩家和ShiftY用侦察机移动吗?谢谢。从您的示例代码中我可以看出,您正在使用渲染方法在shiftX和shiftY处绘制bucky。但是这些变量永远不会在更新方法中更新。尝试分别用buckyPositionX+320和buckyPositionY+160替换这些。如果答案解决了你的问题,请接受。
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;}
if (input.isKeyDown(Input.KEY_UP)){
  rectOne = new Rectangle2D.Float((float)rectOne.getX(), (float)rectOne.getY()+2, (float)90, (float)90);
}