У меня есть код игрового автомата, где он катится по случайным фруктам с maxCount для каждого слота.Как только maxCount будет достигнут, этот слот остановится.Однако иногда слот, метод stop вызывается более одного раза.Есть идеи?
TimerTask
TimerTask task = new TimerTask() {
@Override
public void run() {
runOnUiThread(new TimerTask() {
@Override
public void run() {
count++;
if (!slotOneFinished){
animate(randomSwitchCount(), slotOne, count);
}
if (!slotTwoFinished) {
animate(randomSwitchCount(), slotTwo,count);
}
if (!slotThreeFinished) {
animate(randomSwitchCount(), slotThree,count);
}
}
});
}
};
Функция останова
public void stop(ImageSwitcher slot){
if (slot.equals(slotOne)){
slotOneFinished=true;
Toast.makeText(MainActivity.this, "1",Toast.LENGTH_SHORT).show();
}
if (slot.equals(slotTwo)){
slotTwoFinished=true;
Toast.makeText(MainActivity.this, "2",Toast.LENGTH_SHORT).show();
}
if (slot.equals(slotThree)){
slotThreeFinished=true;
Toast.makeText(MainActivity.this, "3",Toast.LENGTH_SHORT).show();
}
if (slotOneFinished&&slotTwoFinished&&slotThreeFinished){
executor.shutdown();
executor=null;
roll.setEnabled(true);
checkWin(getFruits());
slotOneFinished=false;
slotTwoFinished=false;
slotThreeFinished=false;
}
}
Функция запуска
public void start() {
if(executor==null) {
count =0;
executor = Executors.newSingleThreadScheduledExecutor();
executor.scheduleAtFixedRate(task,10,200,TimeUnit.MILLISECONDS);
}
}
Функция анимации
public void animate(final int maxCount, final ImageSwitcher slot, int 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);
}
}
Счетчик устанавливается в 0 в начале программы, а randomSwitchCount () возвращает случайное число от 10 до 40.Я предполагаю, что у меня нет правильного порядка выполнения некоторого кода.Я использовал тостовые сообщения, чтобы осветить мою проблему во время тестирования.Если есть что-то еще, что вы видите неправильно в моих функциях и логике, я весь в ушах.
Спасибо,
Pi Net