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

2013/2/15

Problems with SimpleTimer...

sqwuckies sqwuckies

2013/2/15

#
public void seeEchidna() { if (getOneIntersectingObject(Echidna.class) != null) { timer.mark(); Greenfoot.playSound ("Here You Are.mp3"); World GameWorld = getWorld(); GameWorld.removeObject(getOneIntersectingObject(Echidna.class)); if (timer.millisElapsed() == 1000) { /Greenfoot.setWorld(new YouLose()); timer.mark(); } } } I am just trying to make it so the World changes when the timer hits 1000 milliseconds. I have put: private SimpleTimer timer = new SimpleTimer(); before my constructors, etc. Am I putting this line in the wrong place or what? Any help would be appreciated. Thanks!
danpost danpost

2013/2/15

#
I do not know if you have one or not, but you need a way to determine if the timer is already running or not. Then in the first 'if' condition add a check making sure it is NOT running; and in the second 'if' condition add a check making sure it IS running.
sqwuckies sqwuckies

2013/2/15

#
I'm not sure what you mean. I do have the 'timer.mark();'s.
danpost danpost

2013/2/15

#
'timer.mark()' sounds like a method to set the begin time of the timer. Without seeing the SimpleTimer class code, I could not say if you have a way through that class to determine if the timer is running or not. You may have to add a instance boolean (not to the SimpleTimer class, but to the class that uses it) to track whether or not it is running.
sqwuckies sqwuckies

2013/2/15

#
Would it help to see the SimpleTimer code? public class SimpleTimer extends Actor { private long lastMark = System.currentTimeMillis(); /** * Marks the current time. You can then in future call * millisElapsed() to find out the elapsed milliseconds * since this mark() call was made. * * A second mark() call will reset the mark, and millisElapsed() * will start increasing from zero again. */ public void mark() { lastMark = System.currentTimeMillis(); } /** * Returns the amount of milliseconds that have elapsed since mark() * was last called. This timer runs irrespective of Greenfoot's * act() cycle, so if you call it many times during the same Greenfoot frame, * you may well get different answers. */ public int millisElapsed() { return (int) (System.currentTimeMillis() - lastMark); } } Author's description of the class: "A simple timer class that allows you to keep track of how much time has passed between events. You use this class by creating a timer as a member field in your actor (or whatever): <pre> private SimpleTimer timer = new SimpleTimer(); </pre> Then when you want to start the timer (for example, when a shot is fired), you call the mark() method: <pre> timer.mark(); </pre> Thereafter, you can use the millisElapsed() method to find out how long it's been since mark() was called (in milliseconds, i.e. thousandths of a second). So if you want to only allow the player to fire a shot every second, you could write: <pre> if (timer.millisElapsed() > 1000 && Greenfoot.isKeyDown("space")) { // Code here for firing a new shot timer.mark(); // Reset the timer } </pre>"
danpost danpost

2013/2/15

#
There is nothing in the class that will allow you to tell if the timer is running or not. Therefore, you will need to use:
// add this field to the class that has the 'seeEchidna' method
boolean timerRunning;
// change your 'seeEchidna' method to the following
public void seeEchidna()
{
    if (!timerRunning && getOneIntersectingObject(Echidna.class) != null)
    {
        timer.mark();
        timerRunning=true;
        Greenfoot.playSound ("Here You Are.mp3");
        World gameWorld = getWorld();
        gameWorld.removeObject(getOneIntersectingObject(Echidna.class));
    }
    if (timerRunning && timer.millisElapsed() == 1000)
    {                
        Greenfoot.setWorld(new YouLose());             
    }                 
}
danpost danpost

2013/2/15

#
Without the check to see if the timer is running or not, the first 'if' statement will return true multiple times, resetting the begin time of the timer each time.
sqwuckies sqwuckies

2013/2/15

#
I see what you mean now. Thanks. How do I set up the boolean statement though? Like what do I put after boolean timerRunning; ?
danpost danpost

2013/2/15

#
You do not need to put anything after it. It is a field declaration statement that should be placed along with your other field declarations/object references.
private SimpleTimer timer = new SimpleTimer();
private boolean timerRunning; // = false by default
sqwuckies sqwuckies

2013/2/15

#
Oh, I figured it out. It works when I put millisElapsed > 1000, but not when millisElapsed == 1000. Thank you so much!
danpost danpost

2013/2/15

#
Of course, I did not catch that when I added the boolean. Good job.
You need to login to post a reply.