lundi 19 février 2018

CountDownTimer + Random Number returns different values on TextSwitcher and logs

I'm writing a game that shows number to player and when such a number is divisible by 3, user has to tap the screen, if number isn't divisible user is doing nothing and waits for next number to show.

Numbers are shown in TextSwitcher and user has 3 secs to take action. Here is main logic:

public CustomTimer gameLoop(int time){
    this.time = time;
    return new CustomTimer(time, time + 5000) {
        @Override
        public void onTick(long millisUntilFinished) {
            //setting level depending on game itaration count
            switch (i) {
                case 10:
                    scopeMin = 49;
                    scopeMax = 200;
                    progressScope = 15;
                    break;
                case 25:
                    scopeMin = 120;
                    scopeMax = 300;
                    progressScope = 20;
                    break;
                case 45:
                    scopeMin = 390;
                    scopeMax = 620;
                    progressScope = 35;
                    break;
                case 80:
                    //disabling progress,because there are no more levels
                    progressBar.setVisibility(View.GONE);
                    nextLevel.setVisibility(View.GONE);
                    break;
            }

            //load random;
            randomNumber = scopeMin + random.nextInt(scopeMax - scopeMin +1);
            //present random to player
            textSwitcher.setText(String.valueOf(randomNumber));
            //progress bar logic
            if (progressBar.getProgress() == progressBar.getMax()) {
                Toast.makeText(MainActivity.this,MainActivity.this.getText(R.string.level_up), Toast.LENGTH_SHORT).show();
                progressStatus = 0;
                progressBar.setProgress(progressStatus);
                progressBar.setMax(progressScope);
            }
            //circle time animation
            animation.start();

        }

        @Override
        public void onFinish() {
            if (!Utils.succesCondition(randomNumber)) {
                //happens then user didn't do anything when he shouldn't
                scoreCount++;
                success();
            } else {
               //happens then user didn't tap the screen, but number was meeting conditions;
                backToStart();
            }
        }
    };
}

@Override
public void onClick (View v){
    if (Utils.succesCondition(randomNumber)) {
        //happens when user taps screen on number that meets conditions;
        loop.cancel();
        scoreCount+= 2;
        vibe.vibrate(50);
        success();
    } else {
        //happens when user taps screen on number that doesn't meets conditions
        backToStart();
    }
}


public void success() {
    scoreView.setText(MainActivity.this.getText(R.string.score) +" "+ String.valueOf(scoreCount));
    if (scoreCount == highScore && scoreCount != 0) {
        Toast.makeText(MainActivity.this, MainActivity.this.getText(R.string.new_record), Toast.LENGTH_SHORT).show();
    }
    if (scoreCount > highScore) {
        editor.putInt(HIGH_SCORE, scoreCount);
        editor.commit();
        highScoreView.setText(MainActivity.this.getText(R.string.high_score) +" "+ String.valueOf(scoreCount));
    }
    vibe.vibrate(25);
    i++;
    progressBar.setProgress(progressStatus +=1);
    loop.start();
    regresBar.clearAnimation();
}

  //returns to startAcvivity, contains info about last shown number and earned score
public void backToStart(){
    loop.cancel();
    Intent i = new Intent(MainActivity.this, StartActivity.class);
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    i.putExtra("scoreKey", String.valueOf(scoreCount));
    i.putExtra("numberKey", randomNumber);
    startActivity(i);
    overridePendingTransition(R.anim.slide_from_left,R.anim.slide_to_right);
    onPause();
    layout.setClickable(false);
}

In onCreate here is simple call:

 Utils.textSwitcherConfiguration(textSwitcher, MainActivity.this);
        loop = gameLoop(time).start();
        layout.setOnClickListener(MainActivity.this);

Problem is:

Sometimes TextSwitcher shows different number than it actually is (based by logs where I've seen different random number than was presented to player). For example player sees number 30, taps the screen and game says he lost because there was number 62...

Also sometimes I can see that when in Text Switcher animation passed number goes away it can change itself to another value, which looks like it's doing it between countdowns...

That obviously makes game unplayable. However that what I noticed is that bug appears way more frequent on slower, older or mid-range devices.




Aucun commentaire:

Enregistrer un commentaire