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

2016/8/16

A code to wait for 10 seconds then proceed to the next code

loxtn10 loxtn10

2016/8/16

#
As the title says I need a code to wait for 10 seconds then proceed to the next code, my case is here, the code happens when ammunition turns to 0 then a text displays "not enough ammo" I would like that text to be displayed in 2 seconds then be removed immediately.I temporarily used the loop of adding 1 to the variable until 10 then proceeds but then it doesn't work.Please help.
if ("z".equals(Greenfoot.getKey())){  
            
                if(meh.ammo != 0)
                {
                      shot2 b = new shot2();
                      getWorld().addObject(b, getX()+10, getY()+10);
                      meh.ammo --;
                }
                else if (meh.ammo == 0 && meh.notenammo != 10 )
                {
                    getWorld().addObject(new NA(), getX(), getY());
                    meh.notenammo ++;
                    if (meh.notenammo == 10){
                    getWorld().removeObjects(getWorld().getObjects(NA.class));
                }
            }
            
        }   
danpost danpost

2016/8/16

#
As your current code is written, ten (10) "z" keyboard inputs would have to register to complete the counter action. Your 'if's should be more like this:
if (meh.ammo == 0)
{
    meh.notenammo++;
    if (meh.notenammo == 550) // about 10 seconds of act cycles
    {
        getWorld().removeObjects(getWorld().getObjects(NA.class));
        meh.ammo = 10;
    }
}
else if ("z".equals(Greenfoot.getKey()))
{
    shot2 b = new shot2();
    getWorld().addObject(b, getX()+10, getY()+10);
    meh.ammo --;
    if (meh.ammo == 0)
    {
        meh.notenammo = 0;
        getWorld().addObject(new NA(), getX(), getY());
    }
}
Now, this can be simplified to using just one counter (since either one int or the other will be active at any time, but never both):
if (meh.ammo > 0)
{
    if ("z".equals(Greenfoot.getKey()))
    {
        getWorld().addObject(new shot2(), getX()+10, getY()+10);
        if (--meh.ammo == 0) getWorld().addObject(new NA(), getX(), getY());
    }
}
else
{
    if (--meh.ammo == -550)
    {
        meh.ammo = 10;
        getWorld().removeObjects(getWorld().getObjects(NA.class));
    }
}
loxtn10 loxtn10

2016/8/18

#
I would like to ask one thing, the code is okay thankss ! but how many here is equivalent to 1 second ? I mean is this a milisecond a millimillisecond?, i just need clarification, sorry I'm not yet a programming student, the prof just gave us project but not digging deeper into programming.
danpost danpost

2016/8/18

#
loxtn10 wrote...
how many here is equivalent to 1 second ? I mean is this a milisecond a millimillisecond?
It is not a matter of milliseconds. It is a matter of act cycles produced by greenfoot while the scenario is in a running state. If the speed slider has not been messed with, a scenario will run at about 55 act cycles per second. So, 10 seconds would be about 550 act cycles (55 acts/second * 10 seconds = 550 acts).
loxtn10 loxtn10

2016/8/18

#
Oh I get it now, so that's what the 550 is for. thanks a lot I get it now, I have another problem but this one is over, the other is here http://www.greenfoot.org/topics/56978/0#post_108166 would llove your support, your comments are pure aids thanks a lot.
You need to login to post a reply.