-1

I have an if statement which checks if three slots are finished. If it is, the timer should stop, but the code is not running for some reason. I have seen a post similar to this If-condition never executes despite correct condition, however their solution solved nothing.

Here is my code:

Stop function

 public void stop(ImageSwitcher slot){
    slotOneFinished = (slot.equals(slotOne));
    slotTwoFinished = (slot.equals(slotTwo));
    slotThreeFinished = (slot.equals(slotThree));
    if (slotOneFinished&&slotTwoFinished&&slotThreeFinished){
        //not running
        Toast.makeText(MainActivity.this, "Running",Toast.LENGTH_SHORT).show();
        checkWin(getFruits());
        timer.cancel();
        timer = null;
    }
}

Timer

private Timer timer;
TimerTask timerTask = new TimerTask() {
    @Override
    public void run() {
        runOnUiThread(new TimerTask() {
            @Override
            public void run() {
                if (!slotOneFinished){
                    animate(randomSwitchCount(), slotOne);
                }
                if (!slotTwoFinished) {
                    animate(randomSwitchCount(), slotTwo);
                }
                if (!slotThreeFinished) {
                    animate(randomSwitchCount(), slotThree);
                }
            }
        });
    }
};

Animate function

public void animate(final int maxCount, final ImageSwitcher slot) {
    i++;
    if (i<maxCount){
        Animation in = AnimationUtils.loadAnimation(this, R.anim.new_slot_item_in);
        Animation out = AnimationUtils.loadAnimation(this, R.anim.old_item_out);
        slot.setInAnimation(in);
        slot.setOutAnimation(out);
        int fruit = randomFruit();
        slot.setTag(fruit);
        slot.setImageResource(fruit);
    }else {
        stop(slot);
    }
}

Using == did nothing as well.

Thanks for your help,

PiNet

Pi Net
  • 69
  • 8

1 Answers1

0

This condition can never be true, assuming that equals() is implemented in the canonical way, and slotOne, slotTwo, and slotThree are 3 distinct objects:

if (slotOneFinished&&slotTwoFinished&&slotThreeFinished)

Looks like you had a mistaken assumption about the scope of the variables. You can probably fix this by using a condition like this, instead:

if( slot == slotOne )
    slotOneFinished = true;

...and so forth.

The Android Studio debugger is your friend.

greeble31
  • 4,894
  • 2
  • 16
  • 30