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

2012/2/21

Image

citadel citadel

2012/2/21

#
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.
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();
            }
        }
    }
}
That is the code for the crab. Thank you very much.
Morran Morran

2012/2/21

#
Is it that you want the crab itself to turn into the image? Or is it that you just want an image to be displayed? If you just want an image to be displayed, create a new class. Call it something like "Decoration". And set its image to "image2.png". When the crab has eaten 10 worms, you use "getWorld().addObject(new Decoration(), getX(), getY())." That could clear up your problems. If you just want it the way it is, but without the lobsters eating your crab, maybe you could do this:
 
    /** 
     * 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");  
                removeAllLobsters(); //this is the only altered part
                Greenfoot.stop();  
            }  
        }  
    }  
    /**
      * RemoveAllLobsters tries to remove all the lobsters so that 
      * they cannot eat the crab.
     */
     public void removeAllLobsters()
     {
        while(getOneIntersectingObject(Lobster.class) != null) {
           Lobster lobster = (Lobster) getOneIntersectingObject(Lobster.class);
           if(lobster != null) {
                getWorld().removeObject(lobster);
           }
        }
     }
Hope that helps you Citadel!
danpost danpost

2012/2/21

#
The one statement
getWorld().removeObjects(getWorld().getObjects(Lobster.class));
will remove any and all lobsters, and will execute even if there are no lobsters in the world.
citadel citadel

2012/2/22

#
Yes I just need the image. Thank you both very much!!!
You need to login to post a reply.