Search code examples
javapong

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


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.


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