Search code examples
javarectangles2d-games

Java 2D game, Both of my rectangles are moving when only one should move


I have coded a simple Java game where there are two rectangles on the screen, one of the rectangles should move and the other should stays still, the moving Rectangle moves with keyboard arrow input and can move either up, down, left or right. The problem I am having is that both rectangles are moving when only one of them should (rectOne) and rectTwo should stay still, I have my variables set up as shown:

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

the coordinates variables buckyPositionX and buckyPositionY are 0,0 so they show the two left corner of my screen and the coordinate variables shiftX and shiftY show the coordinates at the centre of the screen so that the moving rectangle is always centred.

The Play class which has all of the game code inside of it is made up of a few methods, the important ones are update and render, the update deals with the rectangle movement speeds and keyboard inputs to show you an example of what is inside the update class, here is a sample of the code for when they up key is pressed:

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

That is done for all keyboard arrow inputs. Next is the render method which deals with drawing everything on the screen and the graphics, this method includes drawing the rectangles on the screen which were previously coded with the variables.

So, I have two rectangles drawn on the screen when I try move the rectangle with the keyboard input both the rectangles move together at the same time, I think there is a problem with how I have set up my rectangle coordinates and have tried playing about with them for example removing the +buckyPositionX and +buckyPositionY from them but the same problem occurs, if you have any ideas of what may solve this problem please tell me.

Thank you in advance.

Edit1:

Here is my full code for my play class as requested:

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

ANSWERING QUESTIONS:

The reason I used the variables buckyPositionX and buckyPositionY in my rectTwo variables is because when I try to draw this using the g.fillRect function everything works fine with those inputs, rectOne moves and RectTwo stays still but when I call the exact same rectangles from the variables by saying g.fillRect(rectOne.X,rectOne.Y...) I get this problem where both the rectangles are moving together which is strange because the coordinates and the sizes are the exact same as when they are drawn using g.fillRect(500 + buckyPositionX, 330 + buckyPositionY, 210, 150) and same for rectOne. I will try make it more clear with some code:

If I use this in my variables:

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

and this in my render method:

 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());

Both of the rectangles, rectOne and rectTwo move together.

but if I use this in my render method without having any variable set:

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

Everything works VISUALLY although my collisions dont work with this method (which is why I want to fix it) the rectangles both appear on the screen and rectOne moves whilst rectTwo stays still.

I will make a new post explaining my problem in more detail as this one has started getting too crowded and off topic.


Solution

  • I guess this question to related to this?

    Anyway, but rectangles is dependent on buckyPositionY so by updating that variable you are in fact updating both rectangles.

    Try something like this instead. Preferably you would just update the coordinates to rectOne but from what I could see there is no setX method to Rectangle2D

    if (input.isKeyDown(Input.KEY_UP)){
      rectOne = new Rectangle2D.Float((float)rectOne.getX(), (float)rectOne.getY()+2, (float)90, (float)90);
    }