Pong Paddle. Need to carry through the bottom value of count to the top the next time it is initialized

StackOverflow https://stackoverflow.com/questions/21359505

  •  02-10-2022
  •  | 
  •  

Question

public void update(){
            /*
            *  Purpose: Called each frame update (i.e. 30 times a second)
            *  Preconditions: None
            *  Postconditions: return nothing
            */

                int count = 0 ;
                int x = 0;

                if (checkPaddle() == true){
                    count++;
                }



                if (count % 2 == 0) {

                    x = -7;
                    }

                else {
                    x = 7;}


                paddleLocation.y= paddleLocation.y + x;

            }//end update

I want the count that is on the bottom to be the initial value at the top of the method. I can't wrap my head around how to do this.

Was it helpful?

Solution

I suppose you could just make the count a member variable and then you could remove the line int count = 0; to prevent the count from reseting during each update:

int count = 0; // Declare it here or in your constructor

public void update() {
    int x = 0;
    if (checkPaddle() == true) {
        count++;
    }
    if (count % 2 == 0) {
        x = -7;
    } else {
        x = 7;
    }
    paddleLocation.y = paddleLocation.y + x;
}

OTHER TIPS

Aye, I think you want to make count a private field for this.

On an unrelated note; there is some room for improvement in your code. I would personally get rid of all the empty lines, they take up screenspace. Also, you named your change variable x, which is kinda ambiguous. Since you are using y to denote the y position of the paddle, one might think you mean the x-position for x, while in fact you mean the deltaY. (Change in y-pos).

Lastly, checkPaddle() already returns a boolean value, you don't need to check if it equals true. It looks convoluted.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top