my cat needs to sleep and not respond to key moves. But he still responds, I don't understand what I'm doing wrong.
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
*
* FriendlyCat describes cats that
* - move and jump when corresponding control keys are pressed, when having enough energy
* - love pizzas and show their joy by dancing after eating three pizzas
*
*/
public class FriendlyCat extends Cat
{
private int pizzaEaten = 0;
private int energy = 10;
public FriendlyCat()
{
energy = 10;
pizzaEaten = 0;
}
public void act()
{
findPizza(); // call method (use method)
if (Greenfoot.isKeyDown("left"))
{
walkLeft(1); // called method from Cat
energy --; // add each step taken
}
if (Greenfoot.isKeyDown("right"))
{
walkRight(1);
energy --;
}
if (Greenfoot.isKeyDown("down"))
{
jumpDown(1);
energy --;
}
if (Greenfoot.isKeyDown("up"))
{
jumpUp(1);
energy --;
}
if (energy <= 0)
{
sleep (1);
}
}
public void findPizza() // new method (object's interact)
{
if (canSee(Pizza.class))
{
eat();
remove(Pizza.class);
pizzaEaten ++;
howManyEaten();
energy = energy + 5;
}
}
public void howManyEaten()
{
if (pizzaEaten == 3)
{
dance();
pizzaEaten = 0;
}
}
}