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

2015/5/5

Help with displaying messages at start?

rwra rwra

2015/5/5

#
Hey! So i'm working on some code for making a Frogger video game. Right now, i'm working on a section of code that allows the code to display a message when the program runs. Here's what it looks like:
public void Message()
    {
        if (Greenfoot.start())
        {
            bg.drawString("Use the arrow keys to move!", 134, 360);
            bg.drawString("And have Fun!", 134, 50);
        }
    }
I'm still kind of new to this, but its giving me the syntax error (incompatible types) at the end of the parenthesis. Any suggestions on what else i could do for this, or how to fix it? Thanks!
danpost danpost

2015/5/5

#
If you look at the Greenfoot class documentation, you will find that the 'start' method has a 'void' return type (which means it does not return any values). Your line 3, therefore, is equivalent to this;
if ( )
which obviously does not make sense -- nor does it to the compiler; hence, the error. If you read the documentation on the 'start' method, you will find that it is used to start your scenario (put your project in a running state as if you had clicked on the 'Run' button). As such, it can only be used successfully when reachable from your initial world constructor (from code that is executed before the project is started). This, however, is not the method that is of current interest to you. The method you want is located in the World class documentation. Do not use it as an 'if' condition as it also has no returned value (its return type is 'void'). Just override the method in your World subclass (create a method with the same name and return type) and place the code to draw the strings onto the background image in it.
You need to login to post a reply.