I'm working with on one of the little crab scenarios, and I am trying to have it add another lobster and more worms after you eat a set amount of worms. The problem is that I can't get my crab class to call my method "nextLevel();" from my CrabWorld class to add more worms and another lobster. Instead I keep getting an error message saying "non-static method nextLevel() cannot be referenced from a static context. Thanks in advance.
//CRAB CLASS 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; /** * 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(); move(); lookForWorm(); switchImage(); } public Crab() { image1 = new GreenfootImage("crab.png"); image2 = new GreenfootImage("crab2.png"); setImage(image1); } 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(-15); } if (Greenfoot.isKeyDown("right")) { turn(15); } } /** * 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) ) { eat(Worm.class); Greenfoot.playSound("slurp.wav"); wormsEaten = wormsEaten +1; if (wormsEaten == 8) { CrabWorld.nextLevel(); wormsEaten = 0; } } } }
//CrabWorld Class import greenfoot.*; // imports Actor, World, Greenfoot, GreenfootImage import java.util.Random; import java.awt.Color; public class CrabWorld extends World { // public static final Color pathColor = new Color(227, 202, 148); /** * Create the crab world (the beach). Our world has a size * of 560x560 cells, where every cell is just 1 pixel. */ public CrabWorld() { super(560, 560, 1); populateWorld(); } /** * Create the objects for the start of the game. */ public void populateWorld() { addObject(new Crab(), 300, 300); addObject(new Lobster(), 90, 70); addObject(new Lobster(), 390, 200); addObject(new Lobster(), 360, 500); addObject(new Worm(), Greenfoot.getRandomNumber(560), Greenfoot.getRandomNumber(560)); addObject(new Worm(), Greenfoot.getRandomNumber(560), Greenfoot.getRandomNumber(560)); addObject(new Worm(), Greenfoot.getRandomNumber(560), Greenfoot.getRandomNumber(560)); addObject(new Worm(), Greenfoot.getRandomNumber(560), Greenfoot.getRandomNumber(560)); addObject(new Worm(), Greenfoot.getRandomNumber(560), Greenfoot.getRandomNumber(560)); addObject(new Worm(), Greenfoot.getRandomNumber(560), Greenfoot.getRandomNumber(560)); addObject(new Worm(), Greenfoot.getRandomNumber(560), Greenfoot.getRandomNumber(560)); addObject(new Worm(), Greenfoot.getRandomNumber(560), Greenfoot.getRandomNumber(560)); addObject(new Worm(), Greenfoot.getRandomNumber(560), Greenfoot.getRandomNumber(560)); addObject(new Worm(), Greenfoot.getRandomNumber(560), Greenfoot.getRandomNumber(560)); } public void nextLevel() { addObject(new Lobster(), Greenfoot.getRandomNumber(560), Greenfoot.getRandomNumber(560)); addObject(new Worm(), Greenfoot.getRandomNumber(560), Greenfoot.getRandomNumber(560)); addObject(new Worm(), Greenfoot.getRandomNumber(560), Greenfoot.getRandomNumber(560)); addObject(new Worm(), Greenfoot.getRandomNumber(560), Greenfoot.getRandomNumber(560)); addObject(new Worm(), Greenfoot.getRandomNumber(560), Greenfoot.getRandomNumber(560)); addObject(new Worm(), Greenfoot.getRandomNumber(560), Greenfoot.getRandomNumber(560)); addObject(new Worm(), Greenfoot.getRandomNumber(560), Greenfoot.getRandomNumber(560)); addObject(new Worm(), Greenfoot.getRandomNumber(560), Greenfoot.getRandomNumber(560)); addObject(new Worm(), Greenfoot.getRandomNumber(560), Greenfoot.getRandomNumber(560)); addObject(new Worm(), Greenfoot.getRandomNumber(560), Greenfoot.getRandomNumber(560)); addObject(new Worm(), Greenfoot.getRandomNumber(560), Greenfoot.getRandomNumber(560)); addAnimals = false; } }