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

2012/9/10

tutorial help

Stephon231 Stephon231

2012/9/10

#
how do you create a page that comes up when you press run that tells you how to play the game and then have a button to start the game
Cocky Cocky

2012/9/10

#
??? auch auf deutsch
danpost danpost

2012/9/10

#
Create a world sub-class (or an actor sub-class) called 'InfoPage'. If using a world sub-class, pass it a reference to the current world.
Greenfoot.setWorld(new InfoPage(this));
and have the 'InfoPage' world save the reference.
import greenfoot.*;

public class InfoPage extends World
{
    World main = null;

    public InfoPage(World world)
    {
        main = world;
        createImage(); // do not use if you have a default image for this world
    }

    public void act()
    {
        if (Greenfoot.mouseClicked(this)) Greenfoot.setWorld(main);
    }

    // if creating the image programatically
    private void createImage()
    {
        // code creating the image
        // say somewhere: "Click anywhere to continue"
    }
}
Like it says in the 'createImage' method, the image should say to 'click anywhere to continue'.
Stephon231 Stephon231

2012/9/12

#
i don't understand can you please explain a little bit better
danpost danpost

2012/9/12

#
Create a method in your main world (the one that is constructed when compiling or reseting) that calls the alternate world (InfoPage) when the scenario is started. To avoid having it show up after 'Pause'ing and 'Run'ing again, you will need a boolean instance to flag if first start or not. In the main world
// field to flag if first start or not
boolean hasStarted = false;
// method to show 'InfoPage' if first start
public void started()
{
    if (hasStarted) return;
    Greenfoot.setWorld(new InfoPage(this));
    hasStarted = true;
}
'started' is a method in the Greenfoot package within the World API. It is called everytime the scenario goes from a stopped or paused state to a running state. The default method is an empty method (does not do anything). The code in the method above will create and set the new InfoPage as the current world if the scenario is being started for the first time. The InfoPage class in my previous post contains all the code you need for it, except for the creation of the image itself within the 'createImage' method. You will have to put what you want there (or create an image with some other application and set the image here). Alternatively, you could set an image as the default background image and leave the 'createImage' method empty. The code in the 'act' method of 'InfoPage' will return the user to the initial world when the mouse is clicked.
Stephon231 Stephon231

2012/9/12

#
what would i put for: //field to flag if first has started or not and //method to show InfoPage if first start
danpost danpost

2012/9/12

#
The normal layout for a standard class is; (1) imports (2) class declaration (public class ...) (3) instance and class fields (4) constructors (5) methods So, the field would go directly after the class declaration and the method should be placed anywhere after the constructors but not inside any other method. Notice the 'InfoPage' class: it has an instance field 'main' declared in it, plus two methods ('act' and 'createImage').
Stephon231 Stephon231

2012/9/12

#
when i did this it gave me this error message: it was not possible to instantiate a new world the reason for this is usually that the world does not have a public default constructor(a constructor that takes no parameters) or that the world is not a public class
danpost danpost

2012/9/12

#
Create an instance of your main world by right-clicking on the class icon on the right of the screen and selecting one of its constructors (begins with 'new' -- at the top of the options list). If there are none, you need to open the class to edit it and add a constructor to the class; then, go back to step one. By creating an instance of that world, you are setting the initial world to that world. Then, run the scenario.
You need to login to post a reply.