0

我还是 Canvas 的新手。我想要做的是创建一个倒计时,对于每个刻度,都会生成一个随机布尔值。如果为真,show.png 将显示,否则 hide.png 将显示。

基本上,我的游戏是 Whack-A-Mole,我仍在尝试对单个痣进行此操作。到目前为止,这是我的代码,我知道它不会工作,我仍处于“实验”阶段。我将如何改进这一点?如果没有倒计时,画布就会出现,但是当我添加倒计时和“如果出现=真/假”时,它就不起作用了。

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();
    }

}

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

@user387184

我必须再次初始化变量吗?它表示 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

0

试试这样...

在您的主代码中创建并启动倒数计时器:

MyCountDownTimer counter = new MyCountDownTimer(30000,1000);

counter.start();

然后分别创建 CountDownTimer 类:

public class MyCountDownTimer extends CountDownTimer{

public MyCountDownTimer(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}

@Override
public void onFinish() {
...whatever you want to do when finished...
}

@Override
public void onTick(long millisUntilFinished) {
  boolean appear = aRandom.nextBoolean();
  myDrawRoutine(appear);
}
}

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);
             }

}
于 2012-04-12T20:43:11.077 回答