我正在尝试制作一个计时器,每2秒添加一个变量1。所以我使用的是Java计时器,但问题是,它只能在第一次运行,这意味着它只会在变量中加1。
以下是代码:
private Ball[] arr ; // ball array
private int ballNum; // the number of the balls on the screen
private float radius; // the radius of the balls
private int count;// count if to sleep or not
private Timer timer;
public BallCollection(int ballNum) {
this.arr = new Ball [ballNum];
this.ballNum = ballNum;
this.radius = 50;
this.count = 0;
timer = new Timer();
timer.scheduleAtFixedRate(new PachuTask(), 0, 2000);
for(int i = 0; i < arr.length; i++)
this.arr[i] = new Ball(this.radius);
}
private boolean isBumpWithRest(Ball[] few, Ball one) {
for(int i =0;i < this.arr.length; i++) {
if(this.arr[i] != one)
if(this.arr[i].isBump(one))
return true;
}
return false;
}
public void setBalls(Canvas canvas) {
Random rnd = new Random();
for(int i = 0; i < this.ballNum; i++) {
int x = (int) (rnd.nextInt((int)(canvas.getWidth() - 4 * this.radius)) + 2 * this.radius);
int y = (int) (rnd.nextInt((int)(canvas.getHeight() - 4 * this.radius)) + 2 * this.radius);
this.arr[i].setX(x);
this.arr[i].setY(y);
while(this.isBumpWithRest(this.arr, this.arr[i]) && arr[i].getX() != 0) {
x = (int) (rnd.nextInt((int)(canvas.getWidth() - 2 * this.radius)) + this.radius);
y = (int) (rnd.nextInt((int)(canvas.getHeight() - 2 * this.radius)) + this.radius);
this.arr[i].setX(x);
this.arr[i].setY(y);
}
}
}
public void draw(Canvas canvas) {
Paint p = new Paint();
p.setColor(Color.RED);
p.setTextSize(50);
for (int i = 0; i < this.count; i++) {
arr[i].draw(canvas, p);
canvas.drawText("" + this.count, 100, 100, p);
}
}
class PachuTask extends TimerTask {
public void run() {
if(count < arr.length + 1)
count++;
}
}
答案 0 :(得分:1)
尝试使用处理程序实现此目的,它将每2秒调用一次
Runnable runnable1 ,runnable2 = null ;
final Handler handler1 = new Handler();
final Handler handler2 = new Handler();
runnable2 = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
handler1.postAtTime(runnable1, 0);
}
};
runnable1 = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
System.err.println("Called in two second");
handler2.postDelayed(runnable2, 2000);
}
};
handler1.postDelayed(runnable1, 0);
答案 1 :(得分:0)
尝试使用此代码
private Ball[] arr; // ball array
private int ballNum; // the number of the balls on the screen
private float radius; // the radius of the balls
private int count;// count if to sleep or not
private Timer timer;
public BallCollection(int ballNum) {
//check the value of ballNum here.
this.arr = new Ball[ballNum];
this.ballNum = ballNum;
this.radius = 50;
this.count = 0;
for (int i = 0; i < arr.length; i++) {
this.arr[i] = new Ball(this.radius);
}
//check the array size of arr here.
timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
//if (count < arr.length + 1) {
count = count + 1;
//}
System.out.println("" + count);
}
}, 0, 2000);
}
private boolean isBumpWithRest(Ball[] few, Ball one) {
for (int i = 0; i < this.arr.length; i++) {
if (this.arr[i] != one) {
if (this.arr[i].isBump(one)) {
return true;
}
}
}
return false;
}
public void setBalls(Canvas canvas) {
Random rnd = new Random();
for (int i = 0; i < this.ballNum; i++) {
int x = (int) (rnd.nextInt((int) (canvas.getWidth() - 4 * this.radius)) + 2 * this.radius);
int y = (int) (rnd.nextInt((int) (canvas.getHeight() - 4 * this.radius)) + 2 * this.radius);
this.arr[i].setX(x);
this.arr[i].setY(y);
while (this.isBumpWithRest(this.arr, this.arr[i]) && arr[i].getX() != 0) {
x = (int) (rnd.nextInt((int) (canvas.getWidth() - 2 * this.radius)) + this.radius);
y = (int) (rnd.nextInt((int) (canvas.getHeight() - 2 * this.radius)) + this.radius);
this.arr[i].setX(x);
this.arr[i].setY(y);
}
}
}
public void draw(Canvas canvas) {
Paint p = new Paint();
p.setColor(Color.RED);
p.setTextSize(50);
for (int i = 0; i < this.count; i++) {
arr[i].draw(canvas, p);
canvas.drawText("" + this.count, 100, 100, p);
}
}
您不需要创建(扩展)PachuTask
类
请根据您的需要进行修改