1
votes

So we have to make a ball bounce with the code given. Basically the translate method below is run every 1 second because of a timer method in the tester. Also the tester passes dx as 0 and dy as 1. Initially the ball moves down and I am trying to make it bounce back up once it reaches y = 100. I can't figure out why this code would not work because logically it makes sense to me...

public void translate(int dx, int dy) {
    x += dx;
    y += dy;
    if(y >= 100){
        dy = -1 * dy;
    }
  }

I run it with this code, the ball keeps moving down. y = y direction of the ball and x = x direction of the ball

Update: Thanks for the answers. So from what I am getting I need to add the if statements inside the method that calls the translate method. The code for the method calling translate is:

 private void moveMyShape() {

    Timer t = new Timer(DELAY, getActionListener());
    t.start();

  } //method


  private ActionListener getActionListener() {

    return new 
          ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
              myMoveableShape.translate(0, 1);
              myShape.repaint();

            }
          };

  } 

So how would I go about adding if statements in this method? Like how can I keep track of the y position of the ball so I can add the if statements above the translate method. By the way this actionListener code is in a different class. It is in a tester class.

2nd Update: Yes, I have a public static int getY() inside the myMoveableShape. In the tester I added this code:

public void actionPerformed(ActionEvent ae) {
                    if(MyMoveableShape.getY() > 100 || MyMoveableShape.getY() < 0){
                        myMoveableShape.translate(0,-1);
                    }
                    myMoveableShape.translate(0,1);


                myShape.repaint();

But the ball just gets to y = 100 position and stops moving.

2
Consider providing a runnable example which demonstrates your problem. This will result in less confusion and better responses - MadProgrammer
It would be helpful to see the Class of myMoveableShape as well. Does it have a getY() method you could use? Otherwise you could store the velocity inside the object, and then use something more like your original block. - Andrew Alderson
The reason it stops moving at that point is because you both translate it up 1 in the if statement, and then down 1 afterwards. If you changed it to an if/else statement you'd end up just 'vibrating' on 100 instead anyway. - Andrew Alderson
Yea that is exactly what happened. Is there a way around this. Am I even on the right track? - user3754524
Updated my answer, you can give that a whirl. I'd also recommend not making y and its related methods static, as that will cause trouble if you want multiple shapes to be bouncing at the same time. - Andrew Alderson

2 Answers

3
votes

When you pass the int into the translate method it is 'by value', not 'by reference' so any changes you make to the value will not last beyond the scope of the method.

You need to do the the check of y, and the reversal of it, higher up in your code, likely in the same method where you are calling translate from.

Update:

Add a 'velocity' property to your class, something like

private int velocityY = 1;
public int getVelocityY() {
    return velocityY;
}
public void setVelocityY(int vel) {
    velocityY = vel;
}

And then you can modify that block to be something similar to

public void actionPerformed(ActionEvent ae) {
    if(MyMoveableShape.getY() > 100 || MyMoveableShape.getY() < 0){
         myMoveableShape.setVelocityY(-myMoveableShape.getVelocityY());
     }
     myMoveableShape.translate(0,myMoveableShape.getVelocityY());
     myShape.repaint();
}

Update 2: Based on your comments, you could give this a whirl:

private boolean goingUp = false;
public void translate(int dx, int dy) {
    x += dx;
    if(goingUp){
        y += dy;
    } else {
        y -= dy;
    }
    if(y >= 100 || y < 0){
        goingUp = !goingUp; //Toggle it back and forth
    }
  }
1
votes

try putting your if statement above the += statement.

Like:

public void translate(int dx, int dy) 
{
  if(y >= 100)
  {
    dy = -1 * dy;
  }
  x += dx;
  y += dy;
}

I'm sure i'm missing something like are y and x static? need more code to do any better.