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

2014/8/31

Timer issues

GreenGoo GreenGoo

2014/8/31

#
In my game I'm trying to add power-ups. I have imported the SimpleTimer class so that I can ensure that the duration of the power-up is only five seconds, but the timer starts as soon as I compile the world, and even if the latter if statement is true, the power-up remains active.
public void applyPowers() 
    {
        if (pointBoostActive == true || allRedActive == true || invincibilityActive == true || timeSlowActive == true)
        {
            powerUpTimer.mark();
        }
        if(powerUpTimer.millisElapsed() >= 5000) 
        {
            pointBoostActive = false;
            allRedActive = false;
            invincibilityActive = false;
            timeSlowActive = false;
        }
    } 
Okay, I've realised that every act() the powerUpTimer will be marked if any power-ups are active, so that's one problem removed. But why does it start when I compile it, and not when I get a power-up?
danpost danpost

2014/8/31

#
Run-time and real-time are almost never the same. The speed on one system could be different from that on a different one. CPU usage and lag could have an adverse effect on the length of run-time for a power-up. To illustrate in an exaggerated way, let us say that your system decided to start downloading a new update and took about three second to initialize the download. Those three seconds would be 'stolen' from your power-up -- because no 'act' methods were processed during that time (but the system time was still ticking away). Like I said, that was exaggerated, but it brings my point to the fore. Using the system time is not a reliable run-time standard. Better is to count act methods executed. That way, no matter what happens externally of the program, it will always allow the appropriate amount of run-time for each power up. Other than that, each power up type should be dealt with separately -- each with its own act-counting timer.
GreenGoo GreenGoo

2014/8/31

#
Okay thanks, I'll keep that in mind.
You need to login to post a reply.