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

2017/3/30

Timer for a pressing time of an key

Dodo95 Dodo95

2017/3/30

#
Hello, I have a problem with my code. I want to set a timer which asks how long is a key pressed? can you help me? Here is my code:
public void pee() // Dude have to pee 
    {
      Laden world=(Laden) getWorld(); //
        if(Greenfoot.isKeyDown("p")) //if the key "p" is pressed
        {
          timer.mark(); // start Timer 
          if( Greenfoot.isKeyDown("p") && timer.millisElapsed()> 2000) // after 2 sec pressing "p", set new image, remove the object and beerscore -1
           {
            Blase7 blase7 = new Blase7();
            world.addObject(blase7, 1132, 407);
            blase7.setLocation(1125, 400);
          
            getWorld().removeObject(this);
            world.beerscore=world.beerscore-1;
           }

        }
    }
danpost danpost

2017/3/30

#
You are continuously marking the timer during the time the "p" key is pressed. You need to use the condition of the timer -- whether it is running or not -- to determine when to do what.
public void pee()
{
    if (timer.isRunning()) // make sure method name is correct
    {
        if (timer.millisElapsed > 2000)
        {
            Laden world = (Laden) getWorld();
            world.addObject(new Blaze7(), 1125, 400);
            world.removeObject(this);
            world.beerscore--;
        }
        if (!Greenfoot.isKeyDown("p")) timer.stop(); // again, check method name
    }
    else if (Greenfoot.isKeyDown("p")) timer.mark();
}
Dodo95 Dodo95

2017/3/30

#
Are the methods timer.isRunning() and timer.stop(); in the SimpleTimer class?
danpost danpost

2017/3/30

#
Dodo95 wrote...
Are the methods timer.isRunning() and timer.stop(); in the SimpleTimer class?
No -- but, I did not know until now that you were using that particular class. I guess that class is a bit too simple for what you want. Try this, instead (remove the SimpleTImer class from your project):
// instance field
private int timer;

// then
public void pee()
{
    if (timer > 0) // is timer running
    {
        if (--timer == 0) // is time elapsed completely
        {
            Laden world = (Laden) getWorld();
            world.addObject(new Blaze7(), 1125, 400);
            world.removeObject(this);
            world.beerscore--;
        }
        if (!Greenfoot.isKeyDown("p")) timer = 0; // stop timer
    }
    else if (Greenfoot.isKeyDown("p")) timer = 120; // mark timer
}
So, instead of an entire class, you have a simple timer field with a value that is contained within the class and as easy to use as the SimpleTImer object (compare the two sets of codes).
Dodo95 Dodo95

2017/3/30

#
Oh sorry I forgot to tell you that. Thank you very much. Now it works!
Dodo95 Dodo95

2017/3/30

#
I have to continue the code. How can I do it, if I continue pressing the key, every two seconds another image can be set? What are "--" before timer standing for?
danpost danpost

2017/3/30

#
Dodo95 wrote...
What are "--" before timer standing for?
It is a mathematical operator to decrement the value of the variable. This change is effected immediately -- before checking its value to zero, in this case. It is basically the command to run the timer, here.
I have to continue the code. How can I do it, if I continue pressing the key, every two seconds another image can be set?
Since the object is removed after the timer has expired, the creating of another Blaze7 object can only be accomplished by adding another, or the same, dude into the world.
Dodo95 Dodo95

2017/3/30

#
Ok. Thanks for your help!
You need to login to post a reply.