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

2013/2/28

Need help with adding object to the world

jrollins jrollins

2013/2/28

#
I need to create a "penalty" so that when the Crab hits the edge of the world, another Worm is added to a random place in the world. my world is 560x560. Ive tried using addObject but i cant figure out how to make it work in the Crab class and the World wont recognize atWorldEdge. i can provide other codes if needed
public class Crab extends Animal
{
    private GreenfootImage image1;
    private GreenfootImage image2;
    private int wormsEaten;
    private int act;
    
    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();
        lookForWorm();
        switchImage();
    }
    
    /**
     * 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);
        }
        if (Greenfoot.isKeyDown("up"))
        {
            turn(5);
        }
        if (Greenfoot.isKeyDown("down"))
        {
            turn(-5);
        }
    }
    public void switchImage()
    {
        
    act = act + 1;
    if ( act == 3)
    {
      act = 0;
        if ( getImage() == image1 )
    {
        setImage(image2);
    }
    else
    {
        setImage(image1);
    }
    }
}
    
    /**
     * Check whether we have stumbled upon a worm.
     * If we have, eat it. If not, do nothing.
     */
    
    
    public void lookForWorm()
    {
        if ( canSee(Worm.class) ) 
        {
            eat(Worm.class);
            Greenfoot.playSound("slurp.wav");
            
            wormsEaten = wormsEaten + 1;
            if (wormsEaten == 8)
            {
                Greenfoot.playSound("fanfare.wav");
                Greenfoot.stop();
            }
        }
    }
    public void turnAtEdge()
    {
        if (atWorldEdge() )
        {
            turn(42);
            move();
        }
    }
}
danpost danpost

2013/2/28

#
'addObject' is a method in the World class API and needs to be called on a world object. Find a method in the Actor class API that returns the world object that it is in and call 'addObject' on it.
jrollins jrollins

2013/2/28

#
Would i use getWorld? sorry, still not understanding.
danpost danpost

2013/2/28

#
Yes. Take a look at how the Animal class uses it.
jrollins jrollins

2013/2/28

#
ahh! i see now, thank you so very much for your help :)
You need to login to post a reply.