restart button
hi all,
i made a game and i made a restart button and the action for the button is:
else if(evt.getActionCommand().equals("restart")){
Stratego stratego = new Stratego();
}
Stratego is how i run the game. but the problem is when i click the restart button a new window open.
i want to restart the game in the current window how do i do that?
i heard you can hide the current and start a new one but i dont know how
any ideas?
thanks
[502 byte] By [
kimos_cs] at [2007-9-30 10:34:49]

I guess the Stratego constructor creates a window for itself, doesn't it?
So you would need to change that a little bit so that the window constuction is in the constructor, and move all the game initialization to a separate function (called by the constructor). Then when the restart button is pressed you just call the game initialization function so that it restarts the game, instead of creating a new instance of the game.
shmoove
yes Stratego creats a window
this is stratego
/** Creates new form Game of stratego */
public Stratego() {
initComponents();
this.setBounds(0, 0, 600, 650);
setVisible(true);
}
and in the main of stratego class is
/** Start the game */
public static void main(String args[]) {
new Stratego();
}
i made a function like what you said but i got errors
this is the function
public void RestartGame(){
initComponents();
}
i dont know if this is what you meanbut i get the error
MenuKeyListener.java [43:1] non-static method RestartGame() cannot be referenced from a static context
Stratego.RestartGame();
the error is in the class i call the new method
please explane more if you can
You need to have an instance of the "old" statego object. Keep an instance of the Stratego class available to wherever you're key listener is.
Try calling it like so:
statego.RestartGame();
(this might work or not depending on how you're code is structured.
shmoove
i tried
[code] statego.RestartGame();[/code']
but i got the following error.
MenuKeyListener.java [43:1] non-static method RestartGame() cannot be referenced from a static context
and this is becouse initcomponent is a private. i tried to change it to static but it i got alot of errors.
isnt there any other methode? like hiding the current window and starting a new one?
im sorry i dont know how to use [code] as you can seehope you can read my msg :)
so can anyone help me plz?
you shouldn't be re-creating all of your components. You should be modifying the information that deals directly with the actual game. For example, your JFrame will always be there, and it will always hold your game, it never needs to be recreated. However, your "int level" that started at 0 and progressed to 5 by the player's advancing should be reset to 0 again. Maybe you'll reset your character's status and location to go back to start, and you'll re-place the enemies as new enemies where they should be at the beginning. Make sense?
The problem with the static method in non-static context probably means you're doing too much in static methods (like main). Main should do little more then create the initial game, and frame then initialize things to start. The following is a quick example of a way to set up a game and the frame to display it:public class Game {
int level;
ArrayList enemies;
Player player;
GameFrame frame;
public Game()
{
enemies = new ArrayList();
player = new Player();
init();
}
public void init() {
level = 0;
for (int i = enemies.size() - 1; i >= 0; i--) {
((Enemy)enemies.elementAt(i)).reset();
}
player.reset();
}
}
public class GameFrame extends JFrame {
Game model;
public GameFrame(Game initModel) {
model = initModel;
// continue setting up display of the frame
}
// reset button on frame calls model.init();
public static void main(String[] args) {
Game model = new Game();
GameFrame frame = new GameFrame(model);
frame.setVisible(true);
}
}
Now the frame should watch the model and display changes that occur. You could make listeners or utilize existing ones. Or if you don't care as much about OOD, you could simply have a reference in the model back to the frame so it can tell it when it changes. In any case, you'll notice you don't have to create a bunch of new objects to reset the game. Include methods to allow the objects to reset the data inside them, and you definitely will not need to create a new window.
I hope this helps
-JBoeing
