This site requires JavaScript, please enable it in your browser!
Greenfoot back
GunShow124
GunShow124 wrote ...

2014/3/17

For Loop Timer Executing Way Too Fast

GunShow124 GunShow124

2014/3/17

#
Hi there. This is my first post on these forums and need some help with a for loop timer. What I want to happen is to have a for loop as a timer - every 100 steps it should end. What is happening is that either the for loop is being bypassed completely or executing every step. Code is as follows below. In the "Timer" class:
1
2
3
4
5
6
7
8
public boolean runTimer(int userTimerLength)
{
    for (int timerLength = userTimerLength; timerLength > 0; timerLength--)
    {
        return false;
    }
    return true;
}
And this is the method in my world class in which it is being called.
1
2
3
4
5
6
7
8
9
public void addNewCars()
{
    Timer timer = new Timer();
    while(timer.runTimer(100))
    {
        //Don't do stuff
    }
    addNewStuff();
}
Any help would be greatly appreciated. Thanks, C
danpost danpost

2014/3/17

#
Here is what your 'runTimer' method will do: (1) it will accept a value to be placed in a local variable called 'userTimerLength' (2) it will start a loop, setting a new variable 'timerLength' to 'userTimerLenth', check to see if its value is zero or not, and then (since 100 is greater than zero) return a boolean value of 'false'. It will never return a 'true' value; as it will never get passed the first iteration of the loop. Here is what your 'addNewCars' method will do: (1) create a new Timer object (2) 'while(false)' does nothing ('false' returned from method above) (3) call 'addNewStuff' The first two steps, as they are, do nothing as far as the running of your scenario. I have a slight glimmer of hope for you -- maybe. Please post, in its entirety, your Timer class code.
GunShow124 GunShow124

2014/3/17

#
I've done a heavy amount of tweaking since posting this. I don't have the original "Timer" class, but I do have a version working slightly better - only with the slight bug that it makes a vehicle every time the act" method is invoked. This is the new and improved world class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
public class CarWorld extends World
{
    public Counter counter = new Counter();
    private LifeBar lifeBar = new LifeBar();
    private Car car = new Car(lifeBar, this);
    private IntersectionBoundary intBound = new IntersectionBoundary(counter);
    public boolean torf = false;
    private Timer carTimer = new Timer();
 
    /**
     * Constructor for objects of class CarWorld.
     *
     */
 
    public CarWorld()
    {   
        // Create a new world with 600x600 cells with a cell size of 1x1 pixels.
        super(600, 600, 1, false);
 
        setPaintOrder (Car.class, Vehicle.class);
 
        addObject(car, 305, 550);
 
        addObject(new Line(), 300, 0);
        addObject(new Line(), 300, 200);
        addObject(new Line(), 300, 400);
        addObject(new Line(), 300, 600);
 
        addObject(intBound, getWidth()/2, 550);
 
        addObject(counter, 76, 565);
 
        addObject(lifeBar, 512, 40);
        carTimer.initTimer(90);
    }
 
    public void act()
    {
        checkCarTimer();
    }
 
    private void checkCarTimer()
    {
        if (carTimer.checkTimer())
        {
            addObject(new Vehicle(counter, intBound), Greenfoot.getRandomNumber(200) + 200, 0);
            carTimer.initTimer(90);
        }
    }
This is the new and impoved "Timer" class.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
public class Timer 
{
    // instance variables - replace the example below with your own
    private int timerLength;
    private boolean timerFinished = false;
 
    /**
     * Constructor for objects of class Timer
     */
    public Timer()
    {
    }
 
    public void initTimer(int userTimerLength)
    {
        timerLength = userTimerLength;
        runTimer();
    }
     
    private void runTimer()
    {
        timerFinished = false;
        for (int i = timerLength; i <= 0; i--)
        {
            timerFinished = false;
        }
        timerFinished = true;
    }
     
    public boolean checkTimer()
    {
        if (timerFinished == true){
            return true;
        }
        else{
            return false;
        }
    }
}
Thanks, C
danpost danpost

2014/3/17

#
Still, your 'runTimer' method will: (1) reset 'timerFinished' to 'false' (2) run the loop (instantly) (3) set 'timerFinished' to 'true' The total effect: set 'timerFinished' to 'true'. Nothing before (3) accomplished anything as this last statement makes all prior settings of the field during the current execution of the method irrelevant. All your 'runTimer' method should be doing is decreasing the value of the field and setting 'timerFinished' to 'true' if the timer is exhausted (zero). And you should be setting (or re-setting) 'timerFinished' to 'false' in the 'initTimer' method. 'runTimer' should probably not be called from the 'initTimer' method. The 'checkTimer' method can be simplified to:
1
2
3
4
5
6
7
8
9
10
11
public boolean checkTimer()
{
    return timerFinished;
}
// the 'runTimer' method
public void runTimer()
{
    if (timerFinished) return; // do not run, if done
    timerLength--; // run
    if (timerLength == 0) timerFinished = true; // check done
}
GunShow124 GunShow124

2014/3/18

#
Do I then just call the run timer method from the "act" method of the timer? Sorry for the slow reply by the way. I was in a computer science class at school and didn't get home until now. Thanks, C
danpost danpost

2014/3/18

#
GunShow124 wrote...
Do I then just call the run timer method from the "act" method of the timer? Sorry for the slow reply by the way. I was in a computer science class at school and didn't get home until now. Thanks, C
No. I would call it from where it is being used; like you have it in your 'checkCarTimer' method in the CarWorld class.
GunShow124 GunShow124

2014/3/18

#
Ok. Thanks a lot! C
You need to login to post a reply.