2016-11-21 8 views
0

私はカウントダウンタイマーを新しい変数(新しい時間)でリセットしようとしていますが、私は何も動作していないようです。誰でもこれを行う方法を知っていますか?新しいパラメータでカウントダウンタイマーをリセットする

private long cdTime = 30000; 
private long percentageTime = 30; 

private void createCDTimer(){ 
    mCountDownTimer = new CountDownTimer(cdTime, 1000) { 
     public void onTick(long millisUntilFinished) { 
      score = millisUntilFinished/1000; 
      percentage = percentage - (100/percentageTime); 

      cdTime = millisUntilFinished; 
      percentageTime = millisUntilFinished/1000; 
     } 

     public void onFinish() { 
      score = 0; 
      percentage = 0; 
     } 

    }.start(); 
} 

this code is in another class/void { 
    mCountDownTimer.cancel(); 
    cdTime = cdTime + 20000; 
    percentageTime = percentageTime + 20; 
    mCountDownTimer.start(); 
} 

ありがとうございます。

EDIT:

public class GameView extends SurfaceView implements Runnable { 
Context context; 

private Thread gameThread = null; 
private SurfaceHolder ourHolder; 
private volatile boolean playing; 
private boolean paused = true; 
private Canvas canvas; 
private Paint paint; 
private long fps; 
private long thisTimeFrame; 
private int screenX; 
private int screenY; 
private int column1; 
private int column2; 
private int column3; 
private float enemyMDrop; 
private float enemyMY; 
private float enemyLDrop; 
private float enemyLY; 
private float enemyRDrop; 
private float enemyRY; 
private Random randomGen = new Random(); 
private int loadBarAnim; 
private float loadBarTimer; 
private float gameTimer; 
private long percentage = 100; 
private CountDownTimer mCountDownTimer; 
private long cdTime; 
private long percentageTime; 

private Compressor compressor; 
private loadingBar loadBar; 
private loadingBarAnimation loadBarAnimation; 
private EnemyMid enemyM; 
private EnemyLeft enemyL; 
private EnemyLeft enemyR; 

private float mTimer; 
private float lTimer; 
private float rTimer; 
private float enemyMWait; 
private float enemyLWait; 
private float enemyRWait; 
private float enemyMTimer; 
private float enemyLTimer; 
private float enemyRTimer; 
private boolean enemyMGo; 
private boolean enemyLGo; 
private boolean enemyRGo; 

private SoundPool soundPool; 
private int deathSound = -1; 
private int liveSound = -1; 
private int shootSound = -1; 
private int gameMusic = -1; 

private long score; 
String scoreText = "Score: "; 

//String scoreView = String.format("%.02f", score); 
private int lives = 3; 

public GameView (Context context, int x, int y){ 
    super(context); 

    this.context = context; 

    ourHolder = getHolder(); 
    paint = new Paint(); 

    screenX = x; 
    screenY = y; 

    prepeareLevel(); 
} 

private void createCDTimer(){ 
    mCountDownTimer = new CountDownTimer(cdTime, 1000) { 
     public void onTick(long millisUntilFinished) { 
      score = millisUntilFinished/1000; 
      percentage = percentage - (100/percentageTime); 

      cdTime = millisUntilFinished; 
      percentageTime = millisUntilFinished/1000; 
     } 

     public void onFinish() { 
      score = 0; 
      percentage = 0; 
     } 

    }.start(); 
} 

private void prepeareLevel(){ 

    cdTime = 30000; 
    percentageTime = 30; 

    createCDTimer(); 

    loadBarAnim = 1; 

    column1 = screenX/3; 
    column2 = (screenX/3) * 2; 
    column3 = (screenX/3) * 3; 

    compressor = new Compressor(context, screenX, screenY); 
    loadBar = new loadingBar(context, screenX, screenY); 
    loadBarAnimation = new loadingBarAnimation(context, screenX, screenY, 2); 
    enemyM = new EnemyMid(context, screenX, screenY); 
    enemyL = new EnemyLeft(context, screenX, screenY); 
    enemyR = new EnemyLeft(context, screenX, screenY); 


    mTimer = 0; 
    lTimer = 0; 
    rTimer = 0; 

    enemyMTimer = 2; 
    enemyLTimer = 0; 
    enemyRTimer = 5; 

    enemyMGo = false; 
    enemyLGo = false; 
    enemyRGo = false; 

    enemyMDrop = 0; 
    enemyLDrop = 0; 
    enemyRDrop = 0; 

    enemyMWait = randomGen.nextInt((5 - 1) + 1); 
    enemyLWait = randomGen.nextInt((5 - 1) + 1); 
    enemyRWait = randomGen.nextInt((5 - 1) + 1); 
} 

@Override 
public void run(){ 
    while (playing){ 
     long startFrameTime = System.currentTimeMillis(); 

     if(!paused){ 
      update(); 
     } 

     draw(); 

     thisTimeFrame = System.currentTimeMillis() - startFrameTime; 

     if(thisTimeFrame >= 1){ 
      fps = 1000/thisTimeFrame; 
     } 
    } 
} 

private void update(){ 
    boolean finished = false; 

    if (finished){ 

    } 

} 

private void draw(){ 
    if(ourHolder.getSurface().isValid()){ 
     canvas = ourHolder.lockCanvas(); 

     if(rTimer < 30){ 
      rTimer++; 
     } 
     else { 
      rTimer = 0; 

      if (enemyRTimer >= enemyRWait-1){ 
       enemyRGo = true; 
       enemyRTimer = 0; 
      } 
      else if (!enemyRGo){ 
       enemyRTimer++; 
      } 
     } 

     if(mTimer < 30){ 
      mTimer++; 
     } 
     else { 
      mTimer = 0; 
      if (enemyMTimer >= enemyMWait-1){ 
       enemyMGo = true; 
       enemyMTimer = 0; 
      } 
      else if (!enemyMGo){ 
       enemyMTimer++; 
      } 
     } 

     if(lTimer < 30){ 
      lTimer++; 
     } 
     else { 

      if (enemyLTimer >= enemyLWait-1){ 
       enemyLGo = true; 
       enemyLTimer = 0; 
      } 
      else if (!enemyLGo){ 
       enemyLTimer++; 
      } 
     } 

     canvas.drawColor(Color.WHITE); 

     paint.setColor(Color.BLACK); 

     if (enemyMGo){ 
      enemyMDrop = enemyMDrop - 10; 
      enemyMY = enemyM.getY() - enemyMDrop; 
     } 

     if (enemyMY > 1200) { 
      enemyMDrop = 0; 
      enemyMY = 0; 
      enemyMGo = false; 
      enemyMTimer = 0; 
      enemyMWait = randomGen.nextInt((5 - 1) + 1); 

      mCountDownTimer.cancel(); 
      cdTime = cdTime + 20000; 
      percentageTime = percentageTime + 20; 

      mCountDownTimer = new CountDownTimer(cdTime, 1000) { 
       public void onTick(long millisUntilFinished) { 
        score = millisUntilFinished/1000; 
        percentage = percentage - (100/percentageTime); 

        cdTime = millisUntilFinished; 
        percentageTime = millisUntilFinished/1000; 
       } 
       public void onFinish() { 
        score = 0; 
        percentage = 0; 
       } 
      }; 

      mCountDownTimer.start(); 
     } 

     if (enemyLGo){ 
      enemyLDrop = enemyLDrop - 10; 
      enemyLY = enemyL.getY() - enemyLDrop; 
     } 

     if (enemyLY > 1200) { 
      enemyLDrop = 0; 
      enemyLY = 0; 
      enemyLGo = false; 
      enemyLTimer = 0; 
      enemyLWait = randomGen.nextInt((5 - 1) + 1); 
     } 

     if (enemyRGo){ 
      enemyRDrop = enemyRDrop - 10; 
      enemyRY = enemyR.getY() - enemyRDrop; 
     } 

     if (enemyRY > 1200) { 
      enemyRDrop = 0; 
      enemyRY = 0; 
      enemyRGo = false; 
      enemyRTimer = 0; 
      enemyRWait = randomGen.nextInt((5 - 1) + 1); 
     } 

     //Draw the invaders 
     canvas.drawBitmap(enemyL.getBitmap(), (column1 - enemyL.getX()) - (column1/4), enemyLY, paint); 

     canvas.drawBitmap(enemyM.getBitmap(), (column2 - enemyM.getX()) - (column1/4), enemyMY, paint); 

     canvas.drawBitmap(enemyR.getBitmap(), (column3 - enemyL.getX()) - (column1/4), enemyRY, paint); 

     //draw the compressor 
     canvas.drawBitmap(compressor.getBitmap(), 0, 0, paint); 

     //draw the animated loading bar 
     if (loadBarTimer > 2){ 
      loadBarTimer = 0; 
      if (loadBarAnim == 1){ 
       loadBarAnim = 2; 
      } 
      else if (loadBarAnim == 2){ 
       loadBarAnim = 3; 
      } 
      else if (loadBarAnim == 3){ 
       loadBarAnim = 4; 
      } 
      else { 
       loadBarAnim = 1; 
      } 
     } 
     else { 
      loadBarTimer++; 
     } 

     if (loadBarAnim == 1) { 
      canvas.drawBitmap(loadBarAnimation.getBitmap1(), 19, 1300, paint); 
     } 
     else if (loadBarAnim == 2){ 
      canvas.drawBitmap(loadBarAnimation.getBitmap2(), 19, 1300, paint); 
     } 
     else if (loadBarAnim == 3) { 
      canvas.drawBitmap(loadBarAnimation.getBitmap3(), 19, 1300, paint); 
     } 
     else { 
      canvas.drawBitmap(loadBarAnimation.getBitmap4(), 19, 1300, paint); 
     } 

     //draw the loading bar 
     canvas.drawBitmap(loadBar.getBitmap(), 20, 1300, paint); 

     //Draw the Score 
     paint.setColor(Color.BLACK); 
     paint.setTextSize(40); 
     canvas.drawText("Time: " + score, 100, 1600, paint); 

     //Draw the Percentage 
     paint.setColor(Color.BLACK); 
     paint.setTextSize(40); 
     canvas.drawText("Percentage: " + percentage, 725, 1600, paint); 

     ourHolder.unlockCanvasAndPost(canvas); 
    } 
} 

//Paused or Stopped 
public void pause(){ 
    playing = false; 
    try{ 
     gameThread.join(); 
    } 
    catch (InterruptedException e){ 
     Log.e("Error: ", "Error joining thread"); 
    } 
} 

//Resume 
public void resume(){ 
    playing = true; 
    gameThread = new Thread(this); 
    gameThread.start(); 
} 

public boolean onTouchEvent(MotionEvent motionEvent){ 
    switch (motionEvent.getAction() & MotionEvent.ACTION_MASK){ 
     //player touches the screen 
     case MotionEvent.ACTION_DOWN: 
      paused = false; 

      if (motionEvent.getX() > screenX - column1 && enemyRY > 200){ 
       if (motionEvent.getY() < screenY) { 
        enemyRWait = randomGen.nextInt((5 - 1) + 1); 
        enemyRY = 0; 
        enemyRDrop = 0; 
        enemyRGo = false; 
        enemyRTimer = 0; 
        rTimer = 0; 
        canvas = ourHolder.lockCanvas(); 

        canvas.drawColor(Color.WHITE); 

        paint.setColor(Color.BLACK); 

        //Draw the invaders 
        canvas.drawBitmap(enemyL.getBitmap(), (column1 - enemyL.getX()) - (column1/4), enemyLY, paint); 

        canvas.drawBitmap(enemyM.getBitmap(), (column2 - enemyM.getX()) - (column1/4), enemyMY, paint); 

        canvas.drawBitmap(enemyR.getBitmap(), (column3 - enemyL.getX()) - (column1/4), enemyRY, paint); 

        //draw the compressor 
        canvas.drawBitmap(compressor.getBitmap(), 0, 0, paint); 

        //draw the animated loading bar 
        if (loadBarTimer > 2){ 
         loadBarTimer = 0; 
         if (loadBarAnim == 1){ 
          loadBarAnim = 2; 
         } 
         else if (loadBarAnim == 2){ 
          loadBarAnim = 3; 
         } 
         else if (loadBarAnim == 3){ 
          loadBarAnim = 4; 
         } 
         else { 
          loadBarAnim = 1; 
         } 
        } 
        else { 
         loadBarTimer++; 
        } 

        if (loadBarAnim == 1) { 
         canvas.drawBitmap(loadBarAnimation.getBitmap1(), 19, 1300, paint); 
        } 
        else if (loadBarAnim == 2){ 
         canvas.drawBitmap(loadBarAnimation.getBitmap2(), 19, 1300, paint); 
        } 
        else if (loadBarAnim == 3) { 
         canvas.drawBitmap(loadBarAnimation.getBitmap3(), 19, 1300, paint); 
        } 
        else { 
         canvas.drawBitmap(loadBarAnimation.getBitmap4(), 19, 1300, paint); 
        } 

        //draw the loading bar 
        canvas.drawBitmap(loadBar.getBitmap(), 20, 1300, paint); 

        //Draw the Score 
        paint.setColor(Color.BLACK); 
        paint.setTextSize(40); 
        canvas.drawText("Time: " + score, 100, 1600, paint); 

        //Draw the Percentage 
        paint.setColor(Color.BLACK); 
        paint.setTextSize(40); 
        canvas.drawText("Percentage: " + percentage, 725, 1600, paint); 

        ourHolder.unlockCanvasAndPost(canvas); 
       } 
      } 

      if (motionEvent.getX() > screenX - column2 && motionEvent.getX() < screenX - column1 && enemyMY > 200){ 
       if (motionEvent.getY() < screenY) { 
        enemyMWait = randomGen.nextInt((5 - 1) + 1); 
        enemyMY = 0; 
        enemyMDrop = 0; 
        enemyMGo = false; 
        enemyMTimer = 0; 
        mTimer = 0; 
        canvas = ourHolder.lockCanvas(); 

        canvas.drawColor(Color.WHITE); 

        paint.setColor(Color.BLACK); 

        //Draw the invaders 
        canvas.drawBitmap(enemyL.getBitmap(), (column1 - enemyL.getX()) - (column1/4), enemyLY, paint); 

        canvas.drawBitmap(enemyM.getBitmap(), (column2 - enemyM.getX()) - (column1/4), enemyMY, paint); 

        canvas.drawBitmap(enemyR.getBitmap(), (column3 - enemyL.getX()) - (column1/4), enemyRY, paint); 

        //draw the compressor 
        canvas.drawBitmap(compressor.getBitmap(), 0, 0, paint); 

        //draw the animated loading bar 
        if (loadBarTimer > 2){ 
         loadBarTimer = 0; 
         if (loadBarAnim == 1){ 
          loadBarAnim = 2; 
         } 
         else if (loadBarAnim == 2){ 
          loadBarAnim = 3; 
         } 
         else if (loadBarAnim == 3){ 
          loadBarAnim = 4; 
         } 
         else { 
          loadBarAnim = 1; 
         } 
        } 
        else { 
         loadBarTimer++; 
        } 

        if (loadBarAnim == 1) { 
         canvas.drawBitmap(loadBarAnimation.getBitmap1(), 19, 1300, paint); 
        } 
        else if (loadBarAnim == 2){ 
         canvas.drawBitmap(loadBarAnimation.getBitmap2(), 19, 1300, paint); 
        } 
        else if (loadBarAnim == 3) { 
         canvas.drawBitmap(loadBarAnimation.getBitmap3(), 19, 1300, paint); 
        } 
        else { 
         canvas.drawBitmap(loadBarAnimation.getBitmap4(), 19, 1300, paint); 
        } 

        //draw the loading bar 
        canvas.drawBitmap(loadBar.getBitmap(), 20, 1300, paint); 

        //Draw the Score 
        paint.setColor(Color.BLACK); 
        paint.setTextSize(40); 
        canvas.drawText("Time: " + score, 100, 1600, paint); 

        //Draw the Percentage 
        paint.setColor(Color.BLACK); 
        paint.setTextSize(40); 
        canvas.drawText("Percentage: " + percentage, 725, 1600, paint); 

        ourHolder.unlockCanvasAndPost(canvas); 
       } 
      } 

      if (motionEvent.getX() > screenX - column3 && motionEvent.getX() < screenX - column2 && enemyLY > 200){ 
       if (motionEvent.getY() < screenY) { 
        enemyLWait = randomGen.nextInt((5 - 1) + 1); 
        enemyLY = 0; 
        enemyLDrop = 0; 
        enemyLGo = false; 
        enemyLTimer = 0; 
        lTimer = 0; 
        canvas = ourHolder.lockCanvas(); 

        canvas.drawColor(Color.WHITE); 

        paint.setColor(Color.BLACK); 

        //Draw the invaders 
        canvas.drawBitmap(enemyL.getBitmap(), (column1 - enemyL.getX()) - (column1/4), enemyLY, paint); 

        canvas.drawBitmap(enemyM.getBitmap(), (column2 - enemyM.getX()) - (column1/4), enemyMY, paint); 

        canvas.drawBitmap(enemyR.getBitmap(), (column3 - enemyL.getX()) - (column1/4), enemyRY, paint); 

        //draw the compressor 
        canvas.drawBitmap(compressor.getBitmap(), 0, 0, paint); 

        //draw the animated loading bar 
        if (loadBarTimer > 2){ 
         loadBarTimer = 0; 
         if (loadBarAnim == 1){ 
          loadBarAnim = 2; 
         } 
         else if (loadBarAnim == 2){ 
          loadBarAnim = 3; 
         } 
         else if (loadBarAnim == 3){ 
          loadBarAnim = 4; 
         } 
         else { 
          loadBarAnim = 1; 
         } 
        } 
        else { 
         loadBarTimer++; 
        } 

        if (loadBarAnim == 1) { 
         canvas.drawBitmap(loadBarAnimation.getBitmap1(), 19, 1300, paint); 
        } 
        else if (loadBarAnim == 2){ 
         canvas.drawBitmap(loadBarAnimation.getBitmap2(), 19, 1300, paint); 
        } 
        else if (loadBarAnim == 3) { 
         canvas.drawBitmap(loadBarAnimation.getBitmap3(), 19, 1300, paint); 
        } 
        else { 
         canvas.drawBitmap(loadBarAnimation.getBitmap4(), 19, 1300, paint); 
        } 

        //draw the loading bar 
        canvas.drawBitmap(loadBar.getBitmap(), 20, 1300, paint); 

        //Draw the Score 
        paint.setColor(Color.BLACK); 
        paint.setTextSize(40); 
        canvas.drawText("Time: " + score, 100, 1600, paint); 

        //Draw the Percentage 
        paint.setColor(Color.BLACK); 
        paint.setTextSize(40); 
        canvas.drawText("Percentage: " + percentage, 725, 1600, paint); 

        ourHolder.unlockCanvasAndPost(canvas); 
       } 
      } 

      break; 

    } 

    return true; 
} 
} 

エラーコード:

11-22 06:41:

ここに私のコードの残りの部分は、それを必要とする人のためです46.164 9091-9146/au.com.webanddesignbros.loadingbarsimulator W/dalvikvm:threadid = 12:キャッチされていない例外(グループ= 0xa65f7228)でスレッドが終了する
11-22 06:41:46.174 9091-9146/au.com.webanddesignbros.loadingbarsimulator E/AndroidRuntime:致命的除外:スレッド206
java.lang.RuntimeException:呼び出されていないスレッド内でハンドラを作成できませんLooper.prepare()android.os.Handlerで
。(Handler.java:121)
1 android.os.CountDownTimer $で。(CountDownTimer.java:109)android.os.CountDownTimer。(CountDownTimerで
。 java:109)
au.com.webanddesignbros.loadingbarsimulator.GameView $ 2(GameView.java:0)
au.com.webanddesignbros.loadingbarsimulator.GameView.draw(GameView.java:250)
au.com.webanddesignbros.loadingbarsimulator.GameView.run(GameView.java:164)
at java.lang.Thread.run (Thread.java:856)

答えて

0

カウントダウンをキャンセルして、新しい時間値を設定することなく再度開始します。 実際にはcdTimepercentageTimeの変数のみを変更しています。これらの更新された値で新しいカウントダウンタイマーを作成する必要があります。

mCountDownTimer.cancel(); 
cdTime = cdTime + 20000; 
percentageTime = percentageTime + 20; 
//Create a countdown timer with the updated time 
mCountDownTimer = new CounDownTimer(cdTime, anInterval){ 
        /* handle the countdown notifications as you wish, for example like you did in your question */ 
        } 
mCountDownTimer.start(); 

上記のコードが動作するはずであることを検証するため、テストアプリケーションが作成されました。コードとのapkはこのリポジトリから入手できます。https://github.com/ySiggen/CDTimerTest

スタックトレースが質問に追加された後、この答えは、以下で編集されています:

あなたが呼び出している手段を得るエラーバックグラウンドスレッドからのコード(カウントダウンタイマー)をメインスレッドから実行する必要があります。

次のような別のスレッドからコードを呼び出すためにHandlerを使用することができます。

new Handler(Looper.getMainLooper()).post(new Runnable() {   
    @Override 
    public void run() { 
     mCountDownTimer = new CountDownTimer(cdTime, 1000) { 
     /* handle the countdown notifications as you wish, for example like you did in your question */ 
     }.start(); 
    } 
}); 
+0

私はこのアプリのクラッシュをしようとします。 – VeeMo

+0

何が問題なのかを特定するのは難しく、コードのスニペットだけが存在するため、問題を特定するのは難しいです。 – ySiggen

+0

残りを追加しました。 – VeeMo

関連する問題