Hi I'm trying to get greenfoot to display an image after my crab eats 10 worms but what is happening is that the crab itself is turning in to the image and before greenfoot can stop the lobster then eats the crab (bigger image) I need a way to remove the lobster before I change the crab into the picture.
That is the code for the crab.
Thank you very much.
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) /** * This class defines a crab. Crabs live on the beach. They like sand worms * (very yummy, especially the green ones). * * Version: 5 * * In this version, the crab behaves as before, but we add animation of the * image. */ public class Crab extends Animal { private GreenfootImage image1; private GreenfootImage image2; private int wormsEaten; /** * Create a crab and initialize its two images. */ public Crab() { image1 = new GreenfootImage("crab.png"); image2 = new GreenfootImage("crab2.png"); setImage(image1); wormsEaten = 0; } /** * Act - do whatever the crab wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { checkKeypress(); move(); switchImage(); lookForWorm(); } /** * Alternate the crab's image between image1 and image2. */ public void switchImage() { if (getImage() == image1) { setImage(image2); } else { setImage(image1); } } /** * Check whether a control key on the keyboard has been pressed. * If it has, react accordingly. */ public void checkKeypress() { if (Greenfoot.isKeyDown("left")) { turn(-4); } if (Greenfoot.isKeyDown("right")) { turn(4); } } /** * Check whether we have stumbled upon a worm. * If we have, eat it. If not, do nothing. If we have * eaten ten worms, we win. */ public void lookForWorm() { if ( canSee(Worm.class) ) { eat(Worm.class); Greenfoot.playSound("slurp.wav"); wormsEaten = wormsEaten + 1; if (wormsEaten == 10) { Greenfoot.playSound("fanfare.wav"); setRotation(0); setLocation(280, 280); setImage("image2.png"); Greenfoot.stop(); } } } }