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

2019/4/15

What has changed?

TNesler TNesler

2019/4/15

#
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:
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);

    }
}
And here is the MyWorld code snippet I am using to create the ScoreBoard object:
public void gameOver() 
    {
        addObject(new ScoreBoard(), getWidth()/2, getHeight()/2);
    }
Finally, here is the line of code where I get the error message:
if (isTouching(Turtle.class))
    {
        removeTouching(Turtle.class);
        Greenfoot.playSound("game-over.wav");
        MyWorld.gameOver();
    }
Thanks for your help!
nolttr21 nolttr21

2019/4/15

#
You will need to get reference to your world in your Snake class Before line five in the last code snippet you will need
MyWorld world = (MyWorld)getWorld();
then change line five to
world.gameOver();
TNesler TNesler

2019/4/15

#
Ahh, I see that now. Why is the "MyWorld" to the right of the = sign in parentheses? I find it frustrating to have to figure out when an object is in scope or out of scope. For example, I am used to having all objects in a unified hierarchy. In this case the Turtle class can see the Greenfoot class but not the World class...:-( Thanks for the quick reply!
danpost danpost

2019/4/15

#
TNesler wrote...
Why is the "MyWorld" to the right of the = sign in parentheses?
That is so the gameOver method can be found. It is called typecasting. Without it, the MyWorld class will not be searched to find the method (remember that the getWorld method returns a World type reference, not a MyWorld one).
I find it frustrating to have to figure out when an object is in scope or out of scope. For example, I am used to having all objects in a unified hierarchy. In this case the Turtle class can see the Greenfoot class but not the World class...:-(
There is a big difference between the two classes. Notice that ALL the methods in the Greenfoot class are declared static, meaning that they are class methods. NONE of the methods in the World class are static, which means you need an instance of the class to call those methods on. Also, as noted above, the reference to that instance must be, at minimum, cast to a type designated by the class that the method is found in.
You need to login to post a reply.