Kev_T
Kev_T

Reputation: 705

Can't run a function x times

I'm trying to run a function to move a imageView x times. This is a walking animation.

public void doWalk() {
    String mDrawableName = buddy.leraar.naam;
    if (buddy.gezondheid < 30)
    {
        mDrawableName += "_dik_lopen";
    }
    else if (buddy.gezondheid > 85)
    {
        mDrawableName += "_dun_lopen";
    }
    else {
        mDrawableName += "_medium_lopen";
    }
    int resID = getResources().getIdentifier(mDrawableName , "drawable", getPackageName());
    imageView.setImageResource(resID);
    Timer timer = new Timer();

    TimerTask timerTask = new TimerTask() {
        @Override
        public void run() {
            executeWalk(rnd.nextInt(1));
        };
    };
    timer.schedule(timerTask, 300);
}


public void executeWalk(int left) {
    if (rolls < 10)
    {
        rolls++;
        if (left == 0)
        {
            imageView.setX(imageView.getLeft() - 10);
        }
        else {
            imageView.setX(imageView.getLeft() + 10);
        }
    }
}

For some reason the executeWalk function seems to get called only once, while it should run every 300 milliseconds. I have no idea what I'm doing wrong, even after looking at alot of different examples.

Upvotes: 2

Views: 67

Answers (4)

Susmit Agrawal
Susmit Agrawal

Reputation: 3764

You could also use an AsyncTask for this. Extend AsyncTask within the same activity class (make the imageview(s) a class variable)

public class MoveTask extends AsyncTask<Void, Void, Void>{

    @Override
    protected Void doInBackground(Void... voids)
    {
        while(1) {
            delay(300);
            publishProgress();
        }
    }

    @Override
    protected void onProgressUpdate(Void... voids)
    {
        //Move the imageview here
        executeWalk(rnd.nextInt(1));
    }
}

Then in doWalk() make an instance and execute the AsyncTask:

MoveTask moveTask=new MoveTask();
MoveTask.execute();

Upvotes: 0

Guillaume Barr&#233;
Guillaume Barr&#233;

Reputation: 4218

timer.schedule(timerTask, 300); will run the task just once, and the task will run in 300 milliseconds.

In order to repeat the execution you must use :

void schedule (TimerTask task, Date firstTime, long period)

Where the period is the delay between executions.

timer.schedule(timerTask, 300, 100); will run the task the first time in 300 milliseconds and later the task will be launch every 100 milliseconds.

Upvotes: 1

GuessWho
GuessWho

Reputation: 672

Try to use schedule (TimerTask task, long delay, long period). The problem is that you start your function in delay 300ms only one time.

timer.schedule(timerTask, 0, 300);

Upvotes: 3

fmaccaroni
fmaccaroni

Reputation: 3916

According to this the overload of schedule you are using is void schedule(TimerTask task, long delay) so you are just delaying the execution of the task but not performing it periodically. You should use void schedule(TimerTask task, long delay, long period) to schedule the timer to execute something periodically.

So it should be something like:

timer.schedule(timerTask, 0, 300);

Upvotes: 2

Related Questions