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

2012/2/16

make my cat sleep

Tomc84 Tomc84

2012/2/16

#
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; } } }
Morran Morran

2012/2/16

#
Try this:
    public void act()
    {
        findPizza(); // call method (use method)
        if(energy > 0)  
            checkKeys();
        else {
            //do whatever the cat is supposed to do when he's asleep.
        }
    }
    public void checkKeys()
    {
        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 --;
        }
    }
I don't use sleep(long milliseconds) because I can't get it to work for me either.
Tomc84 Tomc84

2012/2/16

#
I still get the same result. he sleeps but still reacts to key press's.
danpost danpost

2012/2/16

#
The best way to fix the problem is to move the 'if (energy <= 0)' segment to the beginning of the act() method with a return statement like so:
private void act()
{
    if (energy <= 0)
    {
        sleep(1)
        return;
    }
    // the rest of your act method
}
The return statement ensures that the rest of the code in your act method is ignored when sleeping.
Tomc84 Tomc84

2012/2/17

#
Works!! awesome thank you. Now if I put in a else statement can I make him see pizza and then interact with it? else{ pizzaEaten(); }
danpost danpost

2012/2/17

#
The 'else' is not neccessary, as any code following the snippet I provided will execute if 'not sleeping'.
You need to login to post a reply.