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

2018/12/20

Making a timer

Fargesia Fargesia

2018/12/20

#
Hi guys, I was wondering how I could make a timer in here?:
if(Greenfoot.isKeyDown("é") && combo<=0){
            setImage(punch);
            combo=150;
            ((Market)getWorld()).kenGetHit();
            Greenfoot.delay(10);
            ((Market)getWorld()).kenStopHit();
        }
I want to replace the delay command by a real timer but I don't know how to do it in this if condition and I'm kind of stuck right now... Thanks a lot
danpost danpost

2018/12/20

#
Fargesia wrote...
I was wondering how I could make a timer in here?: << Code Omitted >> I want to replace the delay command by a real timer
All you need is an int field to count act cycles:
private int punchTimer;
(fields are declared outside of any methods). Then, set the timer to 10 when the punch image is set; decrement it if its value is greater than zero; and restore the image when the timer reaches zero:
if (Greenfoot.isKeyDown("e") && combo <= 0){
    setImage(punch);
    combo = 150;
    ((Market)getWorld()).kenGetHit();
    punchTimer = 10;
}
if (punchTimer > 0 && --punchTimer == 0) ((Market)getWorld()).kenStopHit();
Fargesia Fargesia

2018/12/21

#
Oh I get it thank you so much, seems logic now.
You need to login to post a reply.