You do need a field to control the slow motion portion of the code; however, instead of using two fields (a timer and a flag) using one is easier (a timer; if the timer is not zero, decrement it and if it becomes zero, resume normal speed; if the timer is zero, no action required; when a potato is eaten, set timer to maximum slow time and set slow speed).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | // with instance field private int slowTimer; // in act method runSlowTimer(); eat(); // with these methods private void runSlowTimer() { if (slowTimer > 0 ) { slowTimer--; if (slowTimer == 0 ) Greenfoot.setSpeed( 50 ); } } private void eat() { Actor potato = getOneIntersectingObject(Potato. class ); if (potato != null ) { getWorld().removeObject(potato); slowTimer = 5000 ; Greenfoot.setSpeed( 25 ); } } |