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

2015/4/12

No Animal Class in Little Crab

LuminescentNebula LuminescentNebula

2015/4/12

#
I'm just starting to learn to code, and I'm on chapter two of the Greenfoot book. However, my little crab scenario has no Animal class, which I think is messing up the next exercise (enabling the crab to move). What went wrong, and how can I fix this?
0gener 0gener

2015/4/12

#
Having no Animal Class is not the problem since Crab still inherits the Actor methods... I dont quite get what you are trying to do, but if it's only to make the Crab move, why dont you write in the act(): move(3); // Or whatever number you want.
LuminescentNebula LuminescentNebula

2015/4/13

#
I wasn't aware you had to put a number in the parenthesis, is there a way to make it so it's just move(); ? In the book it says "We can replace the grey text in the middle with a command. One such command is move(); Note that it has to be written exactly as shown, including the parentheses and the semicolon. "
Douggresham Douggresham

2015/4/13

#
The number in parenthesis is the parameter for the method. The move method, which is inherited from the actor class, takes in an integer parameter which specifies how far the object will move in the direction it is facing. I am not sure if move() with no parameter will actually do anything since you are not telling the object how far to move.
danpost danpost

2015/4/14

#
LuminescentNebula wrote...
In the book it says "We can replace the grey text in the middle with a command. One such command is move(); Note that it has to be written exactly as shown, including the parentheses and the semicolon. "
The book was stressing the fact that all method calls must have a pair of parentheses after it (whether there were any values, parameters, in between them or not) and must end with a semi-colon (as all statements must). The 'move();' line given was just an example for show; however, you would then have to add the 'move()' method to the class for it to actually call one. Maybe, for example, something like this:
private void move()
{
    move(3);
    if (atWorldEdge())
    {
        turn(180);
    }
}
LuminescentNebula LuminescentNebula

2015/4/20

#
I understand it now, but the fact that there is no animal class is making it so that I can't make the crab eat worms. How can I make it eat worms?
danpost danpost

2015/4/20

#
LuminescentNebula wrote...
I understand it now, but the fact that there is no animal class is making it so that I can't make the crab eat worms. How can I make it eat worms?
Later versions of greenfoot added some Actor class methods that are similar to what the Animal class had in it. The following table shows what method is equivalent to what:
*  Animal                   Actor           *
*********************************************
canSee(Class)            isTouching(Class)
eat(Class)               removeTouching(Class)
atWorldEdge()            isAtEdge()
danpost danpost

2015/4/21

#
I do not know if the Animal class is still supplied with the greenfood download; but, you could try "Edit>Import class..." from the menubar to see if you could import it into your project. Then just change 'public class Crab extends Actor' to '... extends Animal' to make your project more like the one in the tutorial. Then you will be able to use 'move();' as well as the 'canSee' and 'eat' methods.
You need to login to post a reply.