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

2012/12/26

World Edge

ctgreenfoot ctgreenfoot

2012/12/26

#
So I'm currently learning to program using the Little Crab scenario however I have encountered a simple problem. When the lobster (who is trying to eat the Crab) reaches the edge of the world, it may stay stuck there for a while before moving on. I know that I can fix this with a simple if (atWorldEdge()) command. However, when I put this into the code, it says that it 'cannot find symbol - method atWorldEdge(). What do I do?
erdelf erdelf

2012/12/26

#
maybe you should post your whole lobser class.
danpost danpost

2012/12/26

#
If the class you are placing the 'atWorldEdge' call in is a direct sub-class of Actor (that is, the class declaration has the words 'extends Actor'), or if the super-class (whatever it extends) does not have an 'atWorldEdge' method within it, then you need to create the 'atWorldEdge' method with the pertinent code (or copy it from a class that does have it).
ctgreenfoot ctgreenfoot

2012/12/27

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Lobster here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Lobster extends Animals
{
    /**
     * Act - do whatever the Lobster wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
       
        moveAround();
        eatCrab();
    }  
    private void moveAround()
    {
        move(5);
        if (Greenfoot.getRandomNumber (100) < 10)
            {
                turn(Greenfoot.getRandomNumber (90-45));
            }
        if (getX() <= 5 || getX() >= getWorld() . getWidth() -5)
        {
            turn(180);
        }
        if (getY() <= 5 || getY() >= getWorld() . getHeight() -5)
        {
            turn(180);
        }
        
    }
    private void eatCrab()
    {
        Actor crab;
        crab = getOneObjectAtOffset (0,0,Crab.class);
        if (crab != null)
        {
           World world;
           world = getWorld();
           world.removeObject (crab);
           Greenfoot.playSound ("eating.wav");
           Greenfoot.stop();
        }
    }
}
that is what I have
ctgreenfoot ctgreenfoot

2012/12/27

#
then when I try and write in the atWorldEdge() method, it says that it cannot recognize it
vonmeth vonmeth

2012/12/27

#
        if (getX() <= 5 || getX() >= getWorld() . getWidth() -5)  
        {  
            turn(180);  
        }  
        if (getY() <= 5 || getY() >= getWorld() . getHeight() -5)  
        {  
            turn(180);  
        }  
You already have code that detects if something is at the edge, why do you need the atWorldEdge() method? If you want a atWorldEdge() method, just create it (using the parameters you provided in your own code):
	public boolean atWorldEdge()
	{
    if (getX() <= 5 || getX() >= getWorld() . getWidth() -5)
			return true;
		if (getY() <= 5 || getY() >= getWorld() . getHeight() -5) 
			return true;
		else
			return false;
	}
then:
private void moveAround()  
    {  
        move(5);  
        if (Greenfoot.getRandomNumber (100) < 10)  
            {  
                turn(Greenfoot.getRandomNumber (90-45));  
            }  
		if (atWorldEdge())
			{  
				turn(180);  
			}  
    }  
ctgreenfoot ctgreenfoot

2012/12/27

#
thank you!
You need to login to post a reply.