-1
\$\begingroup\$

I have a SplashScreen class( SplashState) which will show first when someone opens my game, and what i want to do is that my Game will show the SplashState for lets say 5 seconds and after 5 seconds, it will switch to my MenuState. How can I do that(btw im a noob in java, sorry if I got you cringed)

My SplashScreen class

import java.awt.Graphics; public class SplashScreen extends State { private int duration; private boolean running = false; public SplashScreen(Handler handler) { super(handler); } @Override public void render(Graphics g) { g.drawImage(Assets.Splash, 0, 0, 1280, 720, null); } @Override public void tick() { if(handler.getMouseManager().isMiddlePressed()) State.setState(handler.getGame().menuState); } public void run() { } public int getDuration() { return duration; } public void setDuration(int duration) { this.duration = duration; } } 

My State Class(because my splash state extends to this class)

import java.awt.Graphics; import com.vescorspel.game.MyFirstGame.Handler; public abstract class State { private static State currentState = null; public static State setState(State state){ return currentState = state; } public static State getState(){ return currentState; } //CLASS protected Handler handler; public State(Handler handler){ this.handler = handler; } public abstract void tick(); public abstract void render(Graphics g); } 
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

You could do something simple like:

// private variables private long lastTime = System.getCurrentTimeMillis(); //this is how long it has been running for private long totalTime = 0; // Then in the render method long now = System.getCurrentTimeMillis(); long delta = now - lastTime; totalTime += delta; lastTime = now; //Check if 5 seconds has elapsed if(totalTime > 5000) setState(mainmenu); 
\$\endgroup\$
0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.