Dawid Kaim
Dawid Kaim

Reputation: 13

LibGDX trying to create a method that handles click input on a sprite

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


        }
    }
}

Upvotes: 1

Views: 63

Answers (1)

Tobias
Tobias

Reputation: 2575

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.

Upvotes: 1

Related Questions