Java 有问题的倒计时为怪人游戏

Java 有问题的倒计时为怪人游戏,java,android-studio,methods,countdowntimer,Java,Android Studio,Methods,Countdowntimer,我想做一个古怪的游戏。我已启用ImageViews hole1>hole 9。调用swapFace()会按照我的意愿随机交换ImageView一段时间 我想首先调用start(),它将在onTick上调用newPopup()。在长达五秒钟的随机间隔后,newpopup()应调用swapface() 单独调用swapface的工作原理与它应该做的完全一样,但是调用start()不会导致交换。我有一种感觉,我错过了一些明显的东西,我希望能得到一个指针 private void start(){

我想做一个古怪的游戏。我已启用ImageViews hole1>hole 9。调用swapFace()会按照我的意愿随机交换ImageView一段时间

我想首先调用start(),它将在onTick上调用newPopup()。在长达五秒钟的随机间隔后,newpopup()应调用swapface()

单独调用swapface的工作原理与它应该做的完全一样,但是调用start()不会导致交换。我有一种感觉,我错过了一些明显的东西,我希望能得到一个指针

private void start(){

    CountDownTimer myCount = new CountDownTimer(60000, 800){

        @Override
        public void onTick(long millisUntilFinished) {

            newPopup();
        }

        @Override
        public void onFinish(){}
    }; myCount.start();
}

private void newPopup(){

    Random rand = new Random();

    int w = rand.nextInt(5000);

    CountDownTimer myCount3 = new CountDownTimer(w, 100) {
        @Override
        public void onTick(long millisUntilFinished) {

        }

        @Override
        public void onFinish() {

            swapFace();

        }
    };
}

private void swapFace(){

    Random rand = new Random();
    int holeNo = rand.nextInt(9);

    final ImageView x;

    if (holeNo ==0){ x = (ImageView) findViewById(R.id.hole1);}
    else if (holeNo ==1){ x = (ImageView) findViewById(R.id.hole2);}
    else if (holeNo ==2){ x = (ImageView) findViewById(R.id.hole3);}
    else if (holeNo ==3){ x = (ImageView) findViewById(R.id.hole4);}
    else if (holeNo ==4){ x = (ImageView) findViewById(R.id.hole5);}
    else if (holeNo ==5){ x = (ImageView) findViewById(R.id.hole6);}
    else if (holeNo ==6){ x = (ImageView) findViewById(R.id.hole7);}
    else if (holeNo ==7){ x = (ImageView) findViewById(R.id.hole8);}
    else { x = (ImageView) findViewById(R.id.hole9);}

    x.setImageResource(R.drawable.sad);

    int timeUp = rand.nextInt(3000) + 500;

    CountDownTimer myCount = new CountDownTimer(timeUp, 100){

        @Override
        public void onTick(long millisUntilFinished) {

        }
    }
}

我猜您想防止在前一个CountTimer实例完成之前调用
newPopup()
。 您可以使用
synchronized
,或使
myCount
中的勾号大于
w
(5000):

或者介绍一个领域

private newPopupRunning = false; 
并使用它:

private void newPopup(){

    if(newPopupRunning) return; 
    newPopupRunning=true; 

    Random rand = new Random();
    int w = rand.nextInt(5000);

    CountDownTimer myCount3 = new CountDownTimer(w, 100) {
        @Override
        public void onTick(long millisUntilFinished) {

        }

        @Override
        public void onFinish() {

            newPopupRunning=false;
            swapFace();

        }
    };
}
您是否可以在上一个
CountTimer
完成之前调用
newPopup()
private void newPopup(){

    if(newPopupRunning) return; 
    newPopupRunning=true; 

    Random rand = new Random();
    int w = rand.nextInt(5000);

    CountDownTimer myCount3 = new CountDownTimer(w, 100) {
        @Override
        public void onTick(long millisUntilFinished) {

        }

        @Override
        public void onFinish() {

            newPopupRunning=false;
            swapFace();

        }
    };
}