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

2013/4/29

Spinning Crabs

infant17 infant17

2013/4/29

#
Crabs are spinning and don't know why any suggestions..... import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) /** * 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; private int targetWormsEaten; private int wormEnergy; private int wormCount; public int numberOfSteps; /** * 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(); } /** * 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(17); } } /** * 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, the crab is full and cannot eat more worms. *JC.....Function satisfies R6 requirements */ public void lookForWorm() { if ( wormsEaten !=targetWormsEaten&& canSee(Worm.class)) { eat(Worm.class); Greenfoot.playSound("slurp.wav"); wormsEaten = wormsEaten + 1; if (wormsEaten == 10) { return; } } if(wormsEaten <=0) { getWorld().removeObject(this); return; } } public void move() { if(wormsEaten==10) { wormEnergy = (wormEnergy + 20); move(4); wormEnergy = (wormEnergy - 1); wormCount = (wormCount - 1); } } }//END OF CRAB CLASS
danpost danpost

2013/4/29

#
It looks like you have your crab turning randomly about 5 to 6 times a second and up to a 45 degree turn left or right each time. You may want to reduce the change of a turn a bit. And maybe reduce the amount of turn also.
You need to login to post a reply.