I'm trying to pass a value from an actor to the world so that when I create the new actor, I can pass that value to the new actor. However, I get an error stating "non-static method getWormsEaten cannot be referenced from a static context".
Here is the code:
Crab
World
Please help! If there is an easier way to accomplish this task, please let me know! Thanks!
public class Crab extends Animal { private GreenfootImage image1; private GreenfootImage image2; private int wormsEaten = 0; /** * Creates a new crab, assigns both images, and sets wormsEaten to 0. */ public Crab() { image1 = new GreenfootImage("crab.png"); image2 = new GreenfootImage("crab2.png"); setImage(image1); } public void act() { checkKeypress(); lookForWorm(); } /** * Check whether we have stumbled upon a worm. * If we have, eat it, and increase wormsEaten by 1 and add 10 points to the score. * If not, do nothing. * If we have eaten 10 worms in CrabWorld, load CrabWorld2. * If we have eaten 10 worms in CrabWorld2, the game ends. */ public void lookForWorm() { if ( canSee(Worm.class) ) { if (getWorld() instanceof CrabWorld) { ((CrabWorld)getWorld()).getScoreCounter().add(10); } else if(getWorld() instanceof CrabWorld2) { ((CrabWorld2)getWorld()).getScoreCounter().add(10); } eat(Worm.class); Greenfoot.playSound("slurp.wav"); wormsEaten++; if (wormsEaten == 10) { int curscore; int curlives; Greenfoot.playSound("fanfare.wav"); if (getWorld() instanceof CrabWorld) { curscore = ((CrabWorld)getWorld()).getScoreCounter().getValue(); curlives = ((CrabWorld)getWorld()).getLifeCounter().getValue(); Greenfoot.setWorld(new CrabWorld2(curscore, curlives)); } else if(getWorld() instanceof CrabWorld2) { ((CrabWorld2)getWorld()).gameOver(); } } } } /** * Allows user to move the crab using up, down, left, and right arrow keys. * If the crab collides with a rock, the crab cannot proceed. */ public void checkKeypress() { if (Greenfoot.isKeyDown("up")) { setLocation(getX(), getY()-4); switchImage(); if (getOneIntersectingObject(Rock.class) != null) { setLocation(getX(), getY()+4); } } if (Greenfoot.isKeyDown("down")) { setLocation(getX(), getY()+4); switchImage(); if (getOneIntersectingObject(Rock.class) != null) { setLocation(getX(), getY()-4); } } if (Greenfoot.isKeyDown("left")) { move(-4); switchImage(); if (getOneIntersectingObject(Rock.class) != null) { move(4); } } if (Greenfoot.isKeyDown("right")) { move(4); switchImage(); if (getOneIntersectingObject(Rock.class) != null) { move(-4); } } } /** * Switches images as the crab moves. */ public void switchImage() { if ( getImage() == image1 ) { setImage(image2); } else { setImage(image1); } } public int getWormsEaten() { return wormsEaten; } public void setWormsEaten(int worms) { wormsEaten = worms; } }
public class CrabWorld extends World { private ScoreCounter scoreCounter; private LifeCounter lifeCounter; public int numlives = 3; /** * Creates World. The world has a size of 650x650 cells. * Adds a score counter and life counter. */ public CrabWorld() { super(650, 650, 1); populateWorld(); scoreCounter = new ScoreCounter("Score: "); addObject(scoreCounter, 600, 15); lifeCounter = new LifeCounter("Lives: "); addObject(lifeCounter, 600, 27); } /** * Populates world with the player crab, 10 worms, and 3 lobsters. */ public void populateWorld() { addObject( new Crab(), 100, 100 ); addObject( new Worm(), 150, 350); addObject( new Worm(), 200, 50); addObject( new Worm(), 250, 500); addObject( new Worm(), 300, 150); addObject( new Worm(), 350, 400); addObject( new Worm(), 400, 600); addObject( new Worm(), 450, 100); addObject( new Worm(), 500, 450); addObject( new Worm(), 550, 200); addObject( new Worm(), 100, 550); addObject( new Lobster(), 100, 600); addObject( new Lobster(), 550, 100); addObject( new Lobster(), 550, 600); } /** * Returns score counter when switching world or at game over. */ public ScoreCounter getScoreCounter() { return scoreCounter; } /** * Returns life counter when switching worlds. */ public LifeCounter getLifeCounter() { return lifeCounter; } /** * Checks number of lives remaining when crab is eaten. If lives run out, game over. */ public int checkLives() { if(numlives > 0) { int worms; worms = Crab.getWormsEaten(); //here is the problem line addObject(new Crab(setWormsEaten(worms)), 100, 100 ); numlives--; if (numlives == 0) { gameOver(); Greenfoot.stop(); } } return numlives; } /** * Displays game over screen. */ public void gameOver() { addObject(new ScoreBoard(getScoreCounter().getValue()), getWidth()/2, getHeight()/2); } }