1

So this is my code, the problem lies in the timer, I get an error saying that timesClicked is accessed from within inner class, therefore needs to be declared final.

However, if I declare it final, I won't be able to change it. So what do I do?

public static void displayItem(Player Dawid, Item item, int timesClicked, Sprite itemSprite){
    if (Gdx.input.justTouched()) {

        camera.unproject(touchPoint.set(Gdx.input.getX(), Gdx.input.getY(), 0));
        if (Assets.healthPotionSPR.getBoundingRectangle().contains(touchPoint.x, touchPoint.y)) {
            if (timesClicked == 1){
                Shop.checkRequirement(item.getBuyPrice(), Dawid, item);
                Dawid.setGold(Dawid.getGold() - HealthPotion.buyPrice);
                item.setStock(item.getStock()+1);
                topText = "Kupiłeś eliksir zdrowia!";
            }
            Assets.sellText = item.getSellPrice() + "g";
            Assets.buyText = item.getBuyPrice() + "g";
            timesClicked++;
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    timesClicked = 0;
                }
            }, 500);

        }
    }
}

I want to create this method, and I need to be able to provide a static int that would be eddited depending on amounts of clicks and time between each click.

EDIT

ok I got it, so there's a public static int timesClicked, then the method goes :

  public static void displayItem(Player Dawid, Item item, int timesClicked, Sprite itemSprite){

    if (Gdx.input.justTouched()) {

        camera.unproject(touchPoint.set(Gdx.input.getX(), Gdx.input.getY(), 0));
        if (itemSprite.getBoundingRectangle().contains(touchPoint.x, touchPoint.y)) {
            if (timesClicked == 1){
                Shop.checkRequirement(item.getBuyPrice(), Dawid, item);



            }
            Assets.sellText = item.getSellPrice() + "g";
            Assets.buyText = item.getBuyPrice() + "g";
            spriteClicked++;
            timesClicked = spriteClicked;
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    spriteClicked = 0;
                }
            }, 500);


        }
    }
}
Dawid Kaim
  • 13
  • 3
  • Does this answer your question? [Cannot refer to a non-final variable inside an inner class defined in a different method](https://stackoverflow.com/questions/1299837/cannot-refer-to-a-non-final-variable-inside-an-inner-class-defined-in-a-differen) – Antoine Dec 01 '22 at 21:48
  • 1
    Unfortunately not, I am very confused. The problem is I am referring to an undefined int, that will be entered into this method once I call the method, so the solutions in the thread you sent me didn't help me unfortunately – Dawid Kaim Dec 01 '22 at 22:11

1 Answers1

1

You can use a MutableObject or something similar. If you don't use Apache Commons you can simply create one like this:

public class MutableInt {
  public int value;

  public int get() {
    return value;
  }

  public void set(int value) {
    this.value = value;
  }
}

This way you can create a MutableInt and declare it final, but you will still be able to change the int value in it:

// declare this somewhere in the top of your method
final MutableInt clicked = new MutableInt();
clicked.set(timesClicked); // initialize

// ... other code

// replaces "timesClicked++;"
clicked.set(clicked.get() + 1); // or maybe write an "increase" method for this ...

timer.schedule(new TimerTask() {
  @Override
    public void run() {
      // replaces "timesClicked = 0;"
      clicked.set(0); // no problem anymore, because "clicked" is final
    }
  }, 500);

BTW: Maybe I get this wrong, but it seems like you are trying to change the value of timesClicked in the calling method. But timesClicked only exists in the displayItem method, so whenever the method is called again, the value of timesClicked will be reset, so it will never be more than 1.
Maybe try using a global field instead of a method parameter.

EDIT:

If you want to change the value "timesClicked" of another object, you can use the reference of this object instead of the int parameter like this:

public static void displayItem(Player Dawid, Item item, HealthPotionClicked variableHoldingObject, Sprite itemSprite){
  // ...
  variableHoldingObject.setTimesClicked(variableHoldingObject.getTimesClicked() + 1);

  timer.schedule(new TimerTask() {
  @Override
    public void run() {
      variableHoldingObject.setTimesClicked(0);
    }
  }, 500);
}

This way you won't even need the MutableObject.

Tobias
  • 2,547
  • 3
  • 14
  • 29
  • Tobias it is interesting what you've just said. You are correct, timesClicked is taken from an integer from the larger class, e.g "healthPotionClicked", so no matter what I do, I won't be able to change healthPotionClicked in this method? What solution would you suggest in this case? – Dawid Kaim Dec 02 '22 at 07:45
  • @DawidKaim You can use a reference of the "healthPotionClicked" object instead of the integer parameter. I edited the answer to add an example. – Tobias Dec 02 '22 at 08:29
  • Thank you so much Tobias! I made it :) – Dawid Kaim Dec 02 '22 at 17:45