0

我正在做一个游戏。我想创建一个计数器。它从 200 开始,然后 1s 减少了大约 0 。我的代码:

public class GameView extends View{

private int count = 200;
private TimeCountThread timeCountThread;

public GameView(Context context, AttributeSet attrs) {
    super(context, attrs);
    timeCountThread = new TimeCountThread();
            timeCountThread.start();
}
protected void onDraw(Canvas canvas) {
    Paint paint = new Paint();
    paint.setColor(Color.WHITE);
    canvas.drawText("Time :"+count, 10, 35, paint);
}

public class TimeCountThread extends Thread{
    public void run(){
        while(count > 0){
            try {
                sleep(1000);
                count--;
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

public boolean onTouchEvent(MotionEvent event){
    if(event.getAction()==MotionEvent.ACTION_DOWN){
        int x1=(int) event.getX();
        int y1=(int) event.getY();
        Toast.makeText(getContext(), "x1 = "+x1+", y1 ="+y1,1).show();
    }
    return true;
}
}

为什么柜台不活动。请帮帮我?谢谢

4

2 回答 2

1

我想创建一个计数器。它从 200 开始,然后 1s 减少了大约 0

您可以通过CountDownTimer来实现

于 2012-05-24T04:20:09.863 回答
0

尝试使用内部带有可运行对象的处理程序。如:

Handler handler=new Handler();
Runnable r=new Runnable() {
    public void run() {
        count--;
        if(count > 0) {
            handler.postDelayed(this, 1000);
        }              
    }
};
if(count > 0) {
    handler.postDelayed(r, 1000);
}

你在哪里

timeCountThread = new TimeCountThread();
        timeCountThread.start();

是。另一个问题可能是您的计数是私人的,但我认为这不会有那么大的不同。

于 2012-05-24T04:26:28.270 回答