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

2013/4/26

losing energy

1
2
3
infant17 infant17

2013/4/26

#
Need help......I'm having problems with the crab losing 1/20 energy everytime it moves, once the crab use up all its energy it suppose to die but code doesn't work and I don't know why. This what Ive come up with so far: public class Crab extends Animal { private int wormsEaten; private int moves;//JC.....line is an integer variable /** * Do whatever the crab likes to do just now. */ public void act() { if ( atWorldEdge()) { turn(19);//JC.....if the crab sees the edge then the crab turns } if ( Greenfoot.getRandomNumber(100) < 10 ) { turn(Greenfoot.getRandomNumber(90)-45); } move(); if ( canSee(Worm.class)) { eat(Worm.class);//JINELLE......IF CRAB see worm then the crab eats the worm } moves++;//JC line increases the counter once each time the crab moves if (moves == 20)//JC the amount of worm energy the crab can use each time they move { moves = 0; if (wormsEaten > 0) { wormsEaten--; } else { //die?//JC if else statement used is if the number of energy units gets to zero then the crab will die //this function represents } }//JC } }//End of Crab class
infant17 infant17

2013/4/26

#
ANY suggestions on what im doing wrong
danpost danpost

2013/4/26

#
First, I do not see where you increment wormsEaten; and second, you do not have any code in the place where you determine the Crab object is to die.
infant17 infant17

2013/4/26

#
ok thank you
infant17 infant17

2013/4/28

#
danpost or anyone Ive tried to come up ways to make the crab lose the 1/20 of energy and then die but the code ive written still isn't working. Any suggestions on what Im missing or what can be done. I know I have to write a code for the crab to die after it loses all its energy but don't know where to begin with that so I need some help. Count the number of steps by incrementing the numberOfSteps */ public void countSteps() { numberOfSteps = numberOfSteps + 1; } /** * * */ public int getNumberOfSteps() { return numberOfSteps; } /** * ....Stimulates the energy that the crab burns when it moves by subtracting it by the burningRate parameter. * This function satisfies */ public void burnEnergy(int burningRate) { if(numberOfSteps>=burningRate) { numberOfSteps = 0; wormsEaten = wormsEaten - 1; } } public void move() { moves++;//JC line increases the counter once each time the wombat moves if (moves == 0.05)//JC the amount of leaf energy wombat can use each time they move { moves = 0; if (leavesEaten > 0) { leavesEaten--; } else { //die
danpost danpost

2013/4/28

#
It looks like you are complicating things by adding multiple methods and field that are not needed. All you need are the 'leavesEaten' and 'numberOfSteps' fields. Each time a worm is eaten, increment 'wormsEaten'. Each time a legal move is executed, increment 'numberOfSteps' and check its value for 20. If it is 20, reset it to zero and do the following. Decrement 'wormsEaten' and check its value for less than zero. If it is less than zero, execute 'die'.
infant17 infant17

2013/4/28

#
ok danpost I try to do what you said I needed to do even change some coding but getting some errors: this is what I came up with public class Crab extends Animal { private GreenfootImage image1; private GreenfootImage image2; private double wormsEaten; /** * Create a crab and initialize its two images. */ public Crab() { image1 = new GreenfootImage("crab.png"); image2 = new GreenfootImage("crab2.png"); setImage(image1); wormsEaten = Greenfoot.getRandomNumber(23) + 5;//JC.....Sets the intial worms eaten from 5 to 27(energy).......this lines satisfies R5 requirement. } /** * 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() { turnAtEdge(); randomTurn();//JC.....line allows the the crab to move in random directions......line satisfies R4 requirement. move(); lookForWorm(); switchImage(); checkDeath(); } public void act() { if (getObjects(Worm.class)==isEmpty())//JC......line satisfies R13 requirement. } /** * Alternate the crab's image between image1 and image2. */ public void switchImage() { if (getImage() == image1) { setImage(image2); } else { setImage(image1); } } /** * JC.....Check for wheather we are the edge of the world. If we are turn * * */ public void turnAtEdge() { if(atWorldEdge()) { turn(18); } } /** * JC.....Turn randomly from the current direction or dont. If we do turn, turn right or left at a random degree. * Function satisfies line R4 requirement. */ public void randomTurn() { if (Greenfoot.getRandomNumber(100)>90) {turn(Greenfoot.getRandomNumber(90)-45); } } /** * Check whether we have stumbled upon a worm. * If we have, eat it. If not, do nothing. If we have * eaten 10 worms, we win. * .....Function satisfies R6 requirements */ public void lookForWorm() { { eat(Worm.class); Greenfoot.playSound("slurp.wav"); } { if(wormEat) { wormEnergy = (wormEnergy + 20); } public void move() { move(4); wormEnergy = (wormEnergy - 1); } public void checkDeath() { if(wormEnergy=<0) getWorld().removeObject.this; return; wormCount = (wormCount - 1) crabAlive = (crabCount - 1) } public void checkLose() { if(wormCount=<0||crabCount<=0) } } }//END OF CRAB CLASS
danpost danpost

2013/4/28

#
I do not see the 'numberOfSteps' variable anywhere!
infant17 infant17

2013/4/28

#
I accidently took it out I added it back in but getting an error illegal start of type......what does this mean public void checkDeath() { if(wormEnergy=<0) ERROR getWorld().removeObject.this; return; wormCount = (wormCount - 1) crabAlive = (crabCount - 1)
danpost danpost

2013/4/28

#
It usually means that your bracketing is messed up. Please fix the code you have here:
getWorld.removeObject.this:
// should be
getWorld().removeObject(this);
I do not know why you have the last two statements listed above in this method; nor, do I see a reason for them to be there (they should be elsewhere, if anywhere). Well, the 'wormCount' line should be in the 'move' method or a method it calls, after a move is made and 'numberOfSteps' is increments and found to be equals to 20. The last line does not make much sense at all. After removing the last two lines, the 'return' statement will be un-necessary and can be removed also.
infant17 infant17

2013/4/28

#
im still getting error that says illegal start type on line if(wormEnergy=<0).......
infant17 infant17

2013/4/29

#
ERROR incompatible type what does this mean? public void move() { if(wormsEaten) ERROR on this line stating incompatible type { wormEnergy = (wormEnergy + 20); move(4); wormEnergy = (wormEnergy - 1); wormCount = (wormCount - 1); } }
cellostar47 cellostar47

2013/4/29

#
if(wormsEaten) the problem is that wormsEaten returns an int. if() requires something to be true. So you would need to do if(wormsEaten==2) { } The number 2 would be whatever you the if statement to do when that number equals wormsEaten. That should fix the error, unless wormsEaten is not an int....
infant17 infant17

2013/4/29

#
@cellostar47.........why 2 and not another number
infant17 infant17

2013/4/29

#
@cellostar.......ok I understand
There are more replies on the next page.
1
2
3