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

2013/7/21

How to make a Crab each time it moves it uses 1/20 of a Worm worth of energy

1
2
3
4
regba regba

2013/7/22

#
but what i am doing wrong??? what is the difference between public void move() and private void move() Can someone help me? should i replace private void() by public void move() or and add it under public class Crab extends Animal? I have been trying since 0900 this morning non stop :( I know I am almost done, but I don't know what else to do. Thank you
regba wrote...
should i replace private void() by public void move()
Yes, you have to do that, like I said before, when overriding a method, you need to have the same modifier as the original method.
danpost danpost

2013/7/22

#
As someone mentioned before, you already have a 'move()' method in the Animal class (which is the class that your Crab class extends). If you want to override it, you must change it to 'public void move()' instead of 'private void move()'. As an alternative, you can just rename the method to something like 'private void movement()' and change the call in the act from 'move();' to 'movement();'.
regba regba

2013/7/22

#
Ok I have changed public void move() to private void movement() since I already have a 'move()' method in the Animal class and I call the method : movement (); under ACT. This is what what happened now, it start working, I was excited, then as soon as one crab disappears the game stops and I have this error below; R8 is never run though: java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. at greenfoot.Actor.failIfNotInWorld(Actor.java:663) at greenfoot.Actor.getOneObjectAtOffset(Actor.java:867) at Animal.canSee(Animal.java:87) at Crab.lookForWorm(Crab.java:102) at Crab.act(Crab.java:51) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. at greenfoot.Actor.failIfNotInWorld(Actor.java:663) at greenfoot.Actor.getX(Actor.java:157) at Animal.move(Animal.java:60) at Crab.act(Crab.java:51) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. at greenfoot.Actor.failIfNotInWorld(Actor.java:663) at greenfoot.Actor.getX(Actor.java:157) at Animal.move(Animal.java:60) at Crab.act(Crab.java:51) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. at greenfoot.Actor.failIfNotInWorld(Actor.java:663) at greenfoot.Actor.getX(Actor.java:157) at Animal.move(Animal.java:60) at Crab.act(Crab.java:51) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205)
regba regba

2013/7/22

#
Like I said above, R8 never ran: Here is R8: Stop the game once all the Worms have been eaten or all the Crabs have died. Here what I did: * R3 -Set the initial worms eaten (on the Crabs) to a random number between 5 and 11. * R4 -If a Crab has more than 6 worms eaten, it cannot eat any more worms. * R5 -Each time a Crab moves it uses 1/20 of a Worm worth of energy. * R6 -Once a Crab has reached zero Worms eaten, it dies and disappears from the world. * R7 -The Crabs shall move in a random pattern (without key controls) and eat Worms. * R8 -Stop the game once all the Worms have been eaten or all the Crabs have died. * *******************************************************************************************/ /** * 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; // R3. /** * Create a crab and initialize its two images. */ public Crab() { image1 = new GreenfootImage("crab.png"); image2 = new GreenfootImage("crab2.png"); setImage(image1); wormsEaten = 0; wormsEaten= Greenfoot.getRandomNumber(7) +5; // R3 } /** * 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(); movement(); move(); lookForWorm(); switchImage(); randomTurn(); //R7 } public void randomTurn() { if (Greenfoot.getRandomNumber(100) < 10 ) // R7 { turn (Greenfoot.getRandomNumber(90)-45); R7 } } /** * 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 eight worms, we win. */ public void lookForWorm() { if ( canSee(Worm.class) && wormsEaten < 7 ) //R4 { eat(Worm.class); Greenfoot.playSound("slurp.wav"); wormsEaten = wormsEaten + 1; if (wormsEaten > 6) //R4 { move(); //Greenfoot.playSound ("fanfare.wav"); } // if (wormsEaten == 8) { //Greenfoot.playSound("fanfare.wav"); //Greenfoot.stop(); } } } private int moveCount = 0; private void movement() { move(1); // or a 'setLocation' statement moveCount++; if (moveCount == 20) { moveCount = 0; wormsEaten = wormsEaten - 1; if (wormsEaten == 0) { getWorld().removeObject(this); } } } }
danpost danpost

2013/7/22

#
The 'movement' method will remove the crab from the world when out of energy. The call to 'movement' is followed by your call to 'lookForWorm' which uses one of the intersection detection methods that require the object be in the world. Either rearrange the calls in the act method or make sure the actor is in the world before calling any method requiring the actor be in the world (using 'if (getWorld() != null)') after any method that could remove the actor from the world.
regba regba

2013/7/22

#
It still doesn't work I still have the same error. this is what I have in the World: public void act()//R8 { if ( countCrabsInTheWorld() <= 0 || countWormsInTheWorld() <= 0 ) { Greenfoot.stop();// R8 } } public int countCrabsInTheWorld()// R8 { int crabCount;// R8 crabCount = getObjects(Crab.class).size();// R8 return crabCount; //R8 } public int countWormsInTheWorld()// R8 { int wormsCount;//R8 wormsCount = getObjects(Worm.class).size();// R8 return wormsCount; //R8 }
danpost danpost

2013/7/22

#
Anytime you are asking about an error, you should copy/paste what error message (the whole thing) here. Also, use the 'code' tag below the 'Post a reply' box when posting code. When an error is involved, it is better to show the whole class code, so that the location at which the error occurred can be found.
regba regba

2013/7/22

#
Do I really need these codes in the crabworld above? I tried to re-arrange the call method: Here what I have: movement(); lookForWorm(); move(); switchImage(); randomTurn(); I still have these errors below: t greenfoot.Actor.getOneObjectAtOffset(Actor.java:867) at Animal.canSee(Animal.java:87) at Crab.lookForWorm(Crab.java:103) at Crab.act(Crab.java:51) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed.
regba regba

2013/7/22

#
ok. Here is the Error: java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. at greenfoot.Actor.failIfNotInWorld(Actor.java:663) at greenfoot.Actor.getOneObjectAtOffset(Actor.java:867) at Animal.canSee(Animal.java:87) at Crab.lookForWorm(Crab.java:102) at Crab.act(Crab.java:51) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. at greenfoot.Actor.failIfNotInWorld(Actor.java:663) at greenfoot.Actor.getX(Actor.java:157) at Animal.move(Animal.java:60) at Crab.act(Crab.java:51) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. at greenfoot.Actor.failIfNotInWorld(Actor.java:663) at greenfoot.Actor.getX(Actor.java:157) at Animal.move(Animal.java:60) at Crab.act(Crab.java:51) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. at greenfoot.Actor.failIfNotInWorld(Actor.java:663) at greenfoot.Actor.getX(Actor.java:157) at Animal.move(Animal.java:60) at Crab.act(Crab.java:51) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. at greenfoot.Actor.failIfNotInWorld(Actor.java:663) at greenfoot.Actor.getX(Actor.java:157) at Animal.move(Animal.java:60) at Crab.act(Crab.java:51) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. at greenfoot.Actor.failIfNotInWorld(Actor.java:663) at greenfoot.Actor.getOneObjectAtOffset(Actor.java:867) at Animal.canSee(Animal.java:87) at Crab.lookForWorm(Crab.java:103) at Crab.act(Crab.java:51) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. at greenfoot.Actor.failIfNotInWorld(Actor.java:663) at greenfoot.Actor.getOneObjectAtOffset(Actor.java:867) at Animal.canSee(Animal.java:87) at Crab.lookForWorm(Crab.java:103) at Crab.act(Crab.java:51) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205)
danpost danpost

2013/7/22

#
As far as the CrabWorld code, at quick glance, it looked ok (though, the counts will never be less than zero). For the Crab 'act' method call sequence: the 'movement' method is the only one that could remove the crab from the world. So, put it after 'lookForWorm' and 'move' (those method that require the actor be in the world).
regba regba

2013/7/22

#
I put all codes in the crabworld as a comment and do not have anymore errors, will I have the counts less than zero that way? All crabs die, which is good, but never eat all the worms like it is asked(either or) : R8 Stop the game once all the Worms have been eaten or all the Crabs have died.
danpost danpost

2013/7/22

#
regba wrote...
will I have the counts less than zero that way?
No; because it is impossible to have a negative number of any type actor in the world. I was just saying that you did not have to check for values less than zero because of this fact. As an alternative, you could use the 'isEmpty()' method on the 'getObjects(Class)' call, like this:
if (getObjects(Worm.class).isEmpty())
regba regba

2013/7/22

#
When I got rid of the codes in the crabs world everything worked except the game never stop, so I put everything back then it stopped, now the issue is that I need to get the count less than zero, and also as mention above All crabs die, which is good, but never have the option to eat all the worms before they die like it is asked(either or) : R8 Stop the game once all the Worms have been eaten or all the Crabs have died. Thanks
danpost danpost

2013/7/22

#
You 'need to get the count less than zero' of what?
There are more replies on the next page.
1
2
3
4