0
\$\begingroup\$

What I want is that when ever my player left click(mouse click) in the menuState, My init() method in my Game Class must reload. How can i do that???

My game class witn the init() method

public class Game implements Runnable {

private Display display;
private int width,
            height;
public String title;

private boolean running = false;
private Thread thread;

private BufferStrategy bs;
private Graphics g;

//States
public State gameState;
public MenuState menuState;
public SplashScreen splashscreen;

//Input
private KeyManager keyManager;
private MouseManager mouseManager;

//Camera
private GameCamera gameCamera;

//Handler
private Handler handler;

public Game(String title, int width, int height){
    this.width = width;
    this.height = height;
    this.title = title;
    keyManager = new KeyManager();
    mouseManager = new MouseManager();
}

// MY INIT METHOD!!

private void init(){
    display = new Display(title, width, height);
    display.getFrame().addKeyListener(keyManager);
    display.getFrame().addMouseListener(mouseManager);
    display.getFrame().addMouseMotionListener(mouseManager);
    display.getCanvas().addMouseListener(mouseManager);
    display.getCanvas().addMouseMotionListener(mouseManager);
    Assets.init();

    handler = new Handler(this);
    gameCamera = new GameCamera(handler, 0, 0);

    gameState = new GameState(handler);
    menuState = new MenuState(handler);
    splashscreen = new SplashScreen(handler);
    State.setState(splashscreen);
}

private void tick(){
    keyManager.tick();

    if(State.getState() != null);
        State.getState().tick();
}

private void render(){
    bs = display.getCanvas().getBufferStrategy();
    if(bs == null){
        display.getCanvas().createBufferStrategy(3);
        return;
    }
    g = bs.getDrawGraphics();
    //Clear Screen
    g.clearRect(0, 0, width, height);
    //Draw Here!

    if(State.getState() != null);
        State.getState().render(g);

    //End Drawing!
    bs.show();
    g.dispose();
}
//WHEN THE GAME RUNS, it also runs the intit() method and shows fps and tick

public void run(){

    init();

    int fps = 1000;
    double timePerTick = 1000000000 / fps;
    double delta = 0;
    long now;
    long lastTime = System.nanoTime();
    long timer = 0;
    int ticks = 0;

    while(running){
        now = System.nanoTime();
        delta += (now - lastTime) / timePerTick;
        timer += now - lastTime;
        lastTime = now;

        if(delta >= 1){
            tick();
            render();
            ticks++;
            delta--;
        }

        if(timer >= 1000000000){
            System.out.println("Ticks and Frames: " + ticks);
            ticks = 0;
            timer = 0;
        }

    }

    stop();

}

public KeyManager getKeyManager(){
    return keyManager;
}

public MouseManager getMouseManager(){
    return mouseManager;
}

public GameCamera getGameCamera(){
    return gameCamera;
}

public int getWidth(){
    return width;
}

public int getHeight(){
    return height;
}

public synchronized void start(){
    if(running)
        return;
    running = true;
    thread = new Thread(this);
    thread.start();
}

public synchronized void stop(){
    if(!running)
        return;
    running = false;
    try {
        thread.join();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
}
\$\endgroup\$
3
  • \$\begingroup\$ What is it you are stuck on? If it is about reseting your game I am pretty sure you already asked a similar question last week gamedev.stackexchange.com/questions/134277/… \$\endgroup\$ – realUser404 Dec 20 '16 at 10:35
  • \$\begingroup\$ none of those works.. \$\endgroup\$ – SreeLegend Dec 20 '16 at 11:25
  • \$\begingroup\$ I guess you meant "I did not manage to make any of them work". Once again, you need to tell us what you tried and did not work, not ask for a piece of code \$\endgroup\$ – realUser404 Dec 20 '16 at 11:29
0
\$\begingroup\$

Thnx Djindjidj for making my doubt clear on what to do

I made a method called reload() and in that, I call my asset, game camera and gamestate. And in the tick method i put if(handler.getMouseManager().isLeftPressed() && State.getState()==menuState) then it calls my reload()method

private void reload(){
    Assets.init();
    gameState = new GameState(handler);
    gameCamera = new GameCamera(handler, 0, 0);

}

private void tick(){
    if(handler.getMouseManager().isLeftPressed() &&  State.getState()==menuState){
        reload();
    }
\$\endgroup\$
3
  • \$\begingroup\$ I thought you wanted right click ;) \$\endgroup\$ – realUser404 Dec 21 '16 at 15:14
  • \$\begingroup\$ got confused ;) \$\endgroup\$ – SreeLegend Dec 21 '16 at 15:15
  • \$\begingroup\$ right click closes my game \$\endgroup\$ – SreeLegend Dec 21 '16 at 15:15
-2
\$\begingroup\$

The more I think about it, the more it gets clear to me that I do not know enough about your code to be able to give you a working answer. Judging from what you write and wrote in the last post, I guess that you do not know much about the framework, either.

It seems to provide you with a static class "State" that you can store the current state of your game into. It is also called in your tick() and your render()-method to call the tick()- and render()-methods of that current state.

I thus guess that each gamestate has its own assets and game logic. If this is the case, you have two options:

  1. check for a right click in your menustate's tick()-method and call a load()-method from there, reinitializing all your assets (similar to my old post below).
  2. check for a right click and the menu state in the tick()-method of the class you provided here and reinitialize the gamestate object.

You have no dedicated "update"method in your game loop, if not tick() is there for that. Anyways, just prompt your mouse / mouse manager for a right button klick in that method and call the init() method.

Edit:

I have no idea what framework you are using (I suppose you do use one?), but judging from the last post you made, I can derive this:

Am I right in supposing that you want the game to restart when the user clicks the right mouse button while in the menu?

To reset the game, you do not have to reset the display or your handler, you have to reset your game logic and your assets. My first response was thus false, you really don't want to call init() again.

To reset your game, add a new function (could be called "load()") that initializes your game assets, score, game logics etc. Anything that is relevant to gameplay, but not to your engine (like the display or your keyboardmanager). If you want to reset the game to a fresh start without restarting the program, you would have to call that function.

To trigger the load function, use your MouseManager() class. From your last post, I can derive that it should have a function called "isRightPressed()". To restart the game when your right mouse button is clicked and released, do this (for example) in your tick() method:

if(mouseManager().isLeftPressed() && State.getState()==menuState){ //Don't know if this works, I don't know your menustate and State

classes load(); } This will reset your game as you provide it in the "load()-method". You can improve on this by storing the last mouse state and the current to only do this when the right mouse button is released. Else the game will reset each tick as long as you press the right button down. Also, as in the comment, I don't know how you would compare game states in your framework. Do not forget to update your mousemanager in tick(), too.

\$\endgroup\$
2
  • \$\begingroup\$ what should I put in the update()` method??? \$\endgroup\$ – SreeLegend Dec 20 '16 at 11:27
  • \$\begingroup\$ First part of the answer is more confusing than helpful to me \$\endgroup\$ – realUser404 Dec 20 '16 at 11:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.