Hello!
I am working with the Asteroids game found in the Greenfoot book. One of the concepts Introduced is creating a gameover image which is found in the Space "World". I tried to bring that idea to the Trick the Turtle game but I am getting an error on my "Snake" class saying, "Nonstatic method gameOver() cannot be referenced from a static context."
What does this error mean? Except for the fact that I removed the score variable from the ScoreBoard class, I did not change anything. I don't think I missed any other code lines from the Asteroids game. Here is the code for my ScoreBoard class:
And here is the MyWorld code snippet I am using to create the ScoreBoard object:
Finally, here is the line of code where I get the error message:
Thanks for your help!
import greenfoot.*; /** * The ScoreBoard is used to display results on the screen. @version 1.1 */ public class ScoreBoard extends Actor { public static final float FONT_SIZE = 48.0f; public static final int WIDTH = 400; public static final int HEIGHT = 300; /** * Create a score board for the final result. */ public ScoreBoard() { GreenfootImage image = new GreenfootImage(WIDTH, HEIGHT); image.setColor(new Color(255,255,255, 128)); image.fillRect(0, 0, WIDTH, HEIGHT); image.setColor(new Color(0, 0, 0, 128)); image.fillRect(5, 5, WIDTH-10, HEIGHT-10); Font font = image.getFont(); font = font.deriveFont(FONT_SIZE); image.setFont(font); image.setColor(Color.WHITE); image.drawString("Game Over", 60, 100); setImage(image); } }
public void gameOver() { addObject(new ScoreBoard(), getWidth()/2, getHeight()/2); }
if (isTouching(Turtle.class)) { removeTouching(Turtle.class); Greenfoot.playSound("game-over.wav"); MyWorld.gameOver(); }