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

2013/11/12

Counter Help

LugNut29 LugNut29

2013/11/12

#
Hi, I'm having issues with my counter. I need to have my dog fall asleep after the energy hits 0, but mine just continues to count down into the neg numbers. So it will go from 100 down to 0 then continue counting -1, -2, etc. Why is it not putting my dog to sleep when the energy hits 0? Nay help would be great. Here's my code:
public class MyDog2 extends Dog
{
     private String image = "dog.png";
     private int energy = 100;
     private boolean isTired = false;
     private int countSteps = 0;
     
    /**
     * Act - do whatever the MyDog2 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        lookForDog();
        checkKeypress();
        isTired();
        
        countSteps = countSteps += 1;
        if (countSteps == 30)
        {
            energy--;
            countSteps = 0;
        }  
    }
    
    /** 
      * This dog gets sleepy when its energy hits 20 and needs to walk slower. 
      */
    public boolean isTired()
    {
        if ( energy == 0 )
        {
            isTired = true;
            move(-5);
        }
        else
        {
            isTired = false;
        }
        return isTired;
    }
    
    /** 
      * I sleep for a specified length of time and snore too!
      */
    public void sleep(int duration)
    {
        Greenfoot.playSound("Snore1.wav");
        for (int dur = 0; dur <= 10; dur++)
        {
            for (int i = 1; i <=4; i++)
            {
                setImage("dog-sleep-" + i + ".png"); 
                wait(4);
            }
        }
        setImage(image);
        isTired = true;
    }
You need to login to post a reply.