0

So I'm making a simple version of bloons tower defense and I was making a method for the path that the balloons will follow, but it kept telling me that integer than I declared in the method needed to be final or effectively final. I can fix it by using the quick fix which turns it into a final integer array, but why does it need to do this?

Here's my method:

public static void balloonPath(ImageView balloon){
    balloon.toFront();


    int deltaX = 2;
    int deltaY = 0;

    AnimationTimer timer = new AnimationTimer() {
        @Override
        public void handle(long now) {


            if(balloon.getY() == 150 && balloon.getX() == 300){
                deltaX = 0;
                deltaY = 2;
            }
            if(balloon.getY() == 350 && balloon.getX() == 300){
                deltaX = 2;
                deltaY = 0;
            }
            if(balloon.getY() == 350 && balloon.getX() == 450){
                deltaX = 0;
                deltaY = -2;
            }
            if(balloon.getY() == 200 && balloon.getX() == 450){
                deltaX = 2;
                deltaY = 0;
            }
            if(balloon.getY() == 200 && balloon.getX() == 600){
                deltaX = 0;
                deltaY = -2;
            }
            if(balloon.getY() == 50 && balloon.getX() == 600){
                deltaX = 2;
                deltaY = 0;
            }
            if(balloon.getY() == 50 && balloon.getX() == 750){
                deltaX = 0;
                deltaY = 2;
            }
            if(balloon.getY() == 500 && balloon.getX() == 750){
                deltaX = -2;
                deltaY = 0;
            }
            if(balloon.getY() == 500 && balloon.getX() == 450){
                deltaX = 0;
                deltaY = 2;
            }
            if(balloon.getY() == 600 && balloon.getX() == 450){
                deltaX = -2;
                deltaY = 0;
            }
            if(balloon.getY() == 600 && balloon.getX() == 250){
                deltaX = 0;
                deltaY = -2;
            }
            if(balloon.getY() == 450 && balloon.getX() == 250){
                deltaX = -2;
                deltaY = 0;
            }

            balloon.setX(balloon.getX()+deltaX);
            balloon.setY(balloon.getY()+deltaY);

        }
    };
    timer.start();
}

And the error it gives me (there's many for both deltaX and deltaY): Variable 'deltaY' is accessed from within inner class, needs to be final or effectively final Variable 'deltaY' is accessed from within inner class, needs to be final or effectively final

This is the method once I use the quick fix:

public static void balloonPath(ImageView balloon){
    balloon.toFront();


    final int[] deltaX = {2};
    final int[] deltaY = {0};

    AnimationTimer timer = new AnimationTimer() {
        @Override
        public void handle(long now) {


            if(balloon.getY() == 150 && balloon.getX() == 300){
                deltaX[0] = 0;
                deltaY[0] = 2;
            }
            if(balloon.getY() == 350 && balloon.getX() == 300){
                deltaX[0] = 2;
                deltaY[0] = 0;
            }
            if(balloon.getY() == 350 && balloon.getX() == 450){
                deltaX[0] = 0;
                deltaY[0] = -2;
            }
            if(balloon.getY() == 200 && balloon.getX() == 450){
                deltaX[0] = 2;
                deltaY[0] = 0;
            }
            if(balloon.getY() == 200 && balloon.getX() == 600){
                deltaX[0] = 0;
                deltaY[0] = -2;
            }
            if(balloon.getY() == 50 && balloon.getX() == 600){
                deltaX[0] = 2;
                deltaY[0] = 0;
            }
            if(balloon.getY() == 50 && balloon.getX() == 750){
                deltaX[0] = 0;
                deltaY[0] = 2;
            }
            if(balloon.getY() == 500 && balloon.getX() == 750){
                deltaX[0] = -2;
                deltaY[0] = 0;
            }
            if(balloon.getY() == 500 && balloon.getX() == 450){
                deltaX[0] = 0;
                deltaY[0] = 2;
            }
            if(balloon.getY() == 600 && balloon.getX() == 450){
                deltaX[0] = -2;
                deltaY[0] = 0;
            }
            if(balloon.getY() == 600 && balloon.getX() == 250){
                deltaX[0] = 0;
                deltaY[0] = -2;
            }
            if(balloon.getY() == 450 && balloon.getX() == 250){
                deltaX[0] = -2;
                deltaY[0] = 0;
            }

            balloon.setX(balloon.getX()+ deltaX[0]);
            balloon.setY(balloon.getY()+ deltaY[0]);

        }
    };
    timer.start();
}

0 Answers0