Golan Kiviti
Golan Kiviti

Reputation: 4245

Android, java timer works only once

I'm trying to make a timer, to add to a variable 1 every 2 seconds. So I'm using the Java timer, but the problem is, it only works the first time, meaning it only adds 1 to the variable.

Here is the Code:

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

Upvotes: 0

Views: 221

Answers (2)

user3717646
user3717646

Reputation: 436

Try using this code

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


    }
}

And you don't need to create (Extend) PachuTask class

Please modify it with your need

Upvotes: 0

Parag Chauhan
Parag Chauhan

Reputation: 35936

Try to achieve this using handlers,it will call every 2 second itself

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

Upvotes: 1

Related Questions