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

2012/5/9

main menu + keeping the score. help!

Icecold Icecold

2012/5/9

#
I would like to have a main menu in my game where i can start it and where there is another button for instructions. and i also have problems with keeping the score. its a game where the character automaticly goes to the right and the further he moves the higher your score gets. but i cant get that down. help please!!
danpost danpost

2012/5/9

#
For keeping the score: I am assuming that this is a horizontal scroller. Each time a scroll occurs add the movement offset to a variable in the world class. Have the world class act method check its value and add to the score whatever is neccessary for the amount moved (substracting the used portion from the movement value). Let us say your scoring is something like this: for every 100 pixel to the right the player goes 10 points is added to the score. Then the code in the world act would be something like this:
int moveAmount = 0;
Scoreboard scoreboard = new Scoreboard();

public void act()
{
    if (moveAmount >= 100)
    {
        scoreboard.add(10); // the add method in the Scoreboard class should add 10 to the score variable and update the image of the scoreboard object
        moveAmount -= 100;
}
You can check out this simple scoreboard class.
danpost danpost

2012/5/9

#
The following link should help as far as what is neccessary for a start menu. The only thing not discussed there, is how to create the menu. When adding the Button objects, have them receive a String (for the caption as well as for what is returned if clicked on).
public class Button extends Actor
{
    String caption = "";

    public Button(String text)
    {
        caption = text;
        updateImage();
    }

    private void updateImage()
    {
        // code to make the image of the button
    }

    public void act()
    {
        if (Greenfoot.mouseClicked(this))
        {
            MenuWorld mw = (MenuWorld) getWorld();
            mw.menuItem = caption;
             // the last line     OR    the next line
            mw.setMenuItem(caption);
        }
    }
}
Since I have no idea what you named your sub-class of World, I used 'MenuWorld' in the code. You will have to replace those (you could also rename 'mw' to something more appropriate).
Icecold Icecold

2012/5/10

#
thank you very much for your help
martijn13039 martijn13039

2012/5/10

#
hey danpost what is wrong because there is an erro at mw.setMenItem(caption);
import greenfoot.*;



public class Start extends Actor
{

String caption = "";

    public Start(String text)
    {
        caption = text;
        updateImage();
    }

    private void updateImage()
    {
        
    }

    public void act()
    {
        if (Greenfoot.mouseClicked(this))
        {
            World2 mw = (World2) getWorld();
            
            
            mw.setMenuItem(caption);
        }

}
}
Icecold Icecold

2012/5/10

#
private String menuItem = ""; private boolean menuDone = false; public void act() { if (!menuDone && !"".equals(menuItem)) { removeObjects(getObjects(Menu.class)); // set up background for game // add objects for game menuDone = true; } // rest of act (for game play) } public void setMenuItem(String muItem) { menuItem = muItem; } where do i have to write this code? and the code you replied to my question about the main menu?
danpost danpost

2012/5/10

#
All the code provided in your last post should be in your world sub-class. When a button is clicked, it puts its caption in the menuItem variable in the world class, so the world can deal with whatever button was clicked on. The main menu code (look at the code -- the class declaration statement says it all).
Skylander85 Skylander85

2013/7/17

#
thx
You need to login to post a reply.