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

2012/9/8

Little Crab

luna124 luna124

2012/9/8

#
 /**
     * Prüft, ob wir auf einen Wurm gestoßen sind.
     * Wenn ja, wird er gefressen. Wenn nein, passiert nichts.
     */
    public void lookForWorm()
    {
        if(canSee(Worm.class))
        {
            eat(Worm.class);
        }
    }
Hi, I've got a problem: Our teacher gave us the code we have to write and I did as requested but my crab refuses to eat the worm and I don't know where my mistake is. You find my code above. Please don't mind the German part. It means: "Checks, if we can find a worm. If yes, it is eaten. If no, nothing happens. There is no error but it doesn't work. So please help me- it's my homework. Thanks, luna124
danpost danpost

2012/9/8

#
The problem is not is this method. It is either in the 'canSee(class)' or the 'eat(class)' method.
luna124 luna124

2012/9/8

#
Thanks, but I don't know where I can make any changes because I'm a total beginner.
danpost danpost

2012/9/8

#
Can you show what code you have for those methods?
luna124 luna124

2012/9/8

#
 /**
     * Return true if we can see an object of class 'clss' right where we are. 
     * False if there is no such object here.
     */
    public boolean canSee(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0, 0, clss);
        return actor != null;        
    }

    
    /**
     * Try to eat an object of class 'clss'. This is only successful if there
     * is such an object where we currently are. Otherwise this method does
     * nothing.
     */
    public void eat(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0, 0, clss);
        if(actor != null) {
            getWorld().removeObject(actor);
        }
    }
I took this code out of the animal's code. Did you mean this one? I didn't write it myself (of course ;-))
danpost danpost

2012/9/8

#
Then it is not there either. What do you have in your act() method?
luna124 luna124

2012/9/8

#
public void act()
    {
        move();
        if(atWorldEdge())
        {
            turn(17);
        }
        if(Greenfoot.getRandomNumber(100)<20)
        {
            turn(Greenfoot.getRandomNumber(45)-45);
        }
        move();
    }
That's the crab's act method. Maybe you'll find the mistake here...
danpost danpost

2012/9/8

#
I do not see any mistakes here, either. But, also, i do not see any calls to 'lookForWorm'. I do not know if you intended this, but I also see two 'move()' calls in the method.
luna124 luna124

2012/9/8

#
Our teacher told us to call the method "lookForWorm"- not my idea ;-) Do you think the problem are the two "move()" methods?
danpost danpost

2012/9/8

#
I think you misunderstand. By 'call the method', we mean to add a statement in the act to run that method (I believe you are thinking that the name of the method should be 'lookForWorm'). The two move methods will not really present a problem. Although the crab will be traveling twice as fast (when able).
davmac davmac

2012/9/8

#
Putting it another way: your act method calls move() twice, without calling lookForWorm() at all; it should instead call move() only once and should also call lookForWorm() once.
luna124 luna124

2012/9/8

#
??? Please explain, I don't know what you mean...
luna124 luna124

2012/9/8

#
THANK YOU ALL TOGETHER- GOT IT NOW!!!!!!!!!!!!! Have a nice weekend :-D
You need to login to post a reply.