0

Canvas はまだ初心者です。私がやろうとしているのは、ティックごとにランダムなブール値を生成するカウントダウンを作成することです。true の場合は show.png が表示され、そうでない場合は hide.png が表示されます。

基本的に、私のゲームはもぐらたたきです。私はまだこれを 1 つのもぐらにしようとしています。これはこれまでのところ私のコードです。動作しないことはわかっており、まだ「実験中」の段階です。どうすればこれを改善できますか? カウントダウンがないとキャンバスが表示されますが、カウントダウンと「if appear=true/false」を追加すると機能しません。

public class DrawingTheBall extends View{

    Bitmap show;
    Bitmap hide;
    int x;
    int y;

    public DrawingTheBall(Context context) {
        super(context);
        show = BitmapFactory.decodeResource(getResources(), R.drawable.show);
        hide = BitmapFactory.decodeResource(getResources(), R.drawable.hide);
        x = 0;
        y = 0;
    }

    @Override
    protected void onDraw(final Canvas canvas) {
        super.onDraw(canvas);

        final Random aRandom = new Random();

        new CountDownTimer(30000, 1000) {
             public void onTick(long msUntilFinished){

                 boolean appear = aRandom.nextBoolean();
                 if (appear){
                     Paint p = new Paint();
                     canvas.drawBitmap(show, x, y, p);
                 }else{
                     Paint p = new Paint();
                     canvas.drawBitmap(hide, x, y, p);
                 }
             }
             public void onFinish(){

             }
          }.start();

          invalidate();
    }

}

-------------------

@ユーザー387184

変数を再度初期化する必要がありますか? MyCountDownTimer 型のメソッド myDrawRoutine(boolean) がローカルで使用されることはありません。これは私の新しい CountDownTimer.java です:

public class MyCountDownTimer extends CountDownTimer{

    Bitmap show;
    Bitmap hide;
    int x;
    int y;
    Canvas canvas;

    public MyCountDownTimer(int millisInFuture, int countDownInterval) {
        super(millisInFuture, countDownInterval);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onFinish() {
        // TODO Auto-generated method stub
    }

    @Override
    public void onTick(long millisUntilFinished) {
        // TODO Auto-generated method stub
    }

    private void myDrawRoutine(boolean appear) {
        if (appear){
            Paint p = new Paint();
            canvas.drawBitmap(show, x, y, p);
        }else{
            Paint p = new Paint();
            canvas.drawBitmap(hide, x, y, p);
        }
    }
}
4

1 に答える 1