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

2012/2/12

Restart?

1
2
programmer22 programmer22

2012/2/12

#
ok so here is the problem i was trying to get my mouse to be able to click "TRY AGAIN" and then to restart the game but it didnt work so im using keys instead now but what would be a good restart code? like when you press "x" it would just restart the game heres what my section looks like i put in a lil spot for it if u know what to put.
 public void getEaten()
    {
        //  Code for sound (if you want)
        //  Code to remove crab (if you want)
        CrabWorld gw = (CrabWorld) getWorld();
        ScoreBoard scoreboard = new ScoreBoard(counter.getValue());   
        gw.addObject(scoreboard, 273, 302);
        gw.repaint();
        while(!Greenfoot.isKeyDown("right")){}
        //insert program restart here
        
        Greenfoot.stop();
    }
danpost danpost

2012/2/12

#
The easiest way is to put code in the world class that looks to see if a crab is in the world and if not remove all objects a re-prepare the world, if the user clicks a 'Try again' button.
// In the world act() method
if (getObjects(Crab.class) == null)
{
    if (getObjects(Button.class) == null) addObject(new Button(), [centerX], [centerY])
    return;
}
Then in the button class check for mouseClick, and when clicked, call the world prepare method, reset variables, and remove button (you could have a seperate method for initializing variables in the world class and call it).
danpost danpost

2012/2/12

#
This would mean lines 8 through 12 in the code above should be replaced by
gw.removeObject(this);
return;
programmer22 programmer22

2012/2/12

#
ok give me a second gotta make a button class >.> lol
programmer22 programmer22

2012/2/12

#
umm the first part of the code you gave me im unsure where to put that nvm >.> feel like a retard
programmer22 programmer22

2012/2/12

#
ok so i put the first part in the world act method but it says its an illeagle start of expression right before
danpost danpost

2012/2/12

#
Yeah, I put them in brackets so, I hoped, you would know to replace them with getWidth() / 2 and getHeight() / 2.
programmer22 programmer22

2012/2/12

#
ok so i fixed that onto checking if mouse is clicked gotta check the api
programmer22 programmer22

2012/2/12

#
would i do get button?
programmer22 programmer22

2012/2/12

#
or wait this getMouseInfo
programmer22 programmer22

2012/2/12

#
or wait this getMouseInfo
programmer22 programmer22

2012/2/12

#
ok so i just made the button and put get mouse info in it should i put the button on the try again spot so when you click it it will restart ?
programmer22 programmer22

2012/2/12

#
your code makes so much sense reading it lol but when i try to figure out how to put it together in code im like how? lol so close yet so far away
danpost danpost

2012/2/12

#
No, it is in the Greenfoot API. Just use
public void act()
    if (Greenfoot.mouseClicked(this))
    {
        CrabWorld gw = (CrabWorld) getWorld();
        gw.addObject(new Crab(), [spawnX], [spawnY]); // replace what is in square brackets
        gw.prepare();
        gw.resetVariables(); // you might have to create this
        gw.removeObject(this);
    }
}
danpost danpost

2012/2/12

#
The button class can be fairly simple
import greenfoot.*;
import java.awt.Color;

public class Button extends Actor
{
    public Button()
    {
        setImage(new GreenfootImage(" Try again ", 30, Color.BLACK, Color.RED));
    }

    public void act() // as above
    //  etc.
}
There are more replies on the next page.
1
2