Reputation: 85
been stuck on this annoying issue for the past 1 - 2 hours already. Was wondering if someone could help me out. So I added a splash screen for my soon to be finished game. I was following this tutorial I found online http://www.caindev.com/2012/06/04/libgdx-splash-screen-image-tutorial.html it was exactly what I wanted. I needed a splash screen to load and stay until the user pressed the screen. I did everything the tutorial said to do but once the image from my splash screen is loaded it won't switch to my MainMenu screen when it's pressed the image just stays there frozen.
Here's my code for: MyGame
package com.jack.mygame;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.jack.MyGame.screens.GameScreen;
import com.jack.MyGame.splash.SplashScreen;
public class MyGame extends Game {
public static int nWidth;
public static int nHeight;
private Game game;
public MyGame() {
game = this;
}
@Override
public void create() {
nWidth = Gdx.graphics.getWidth();
nHeight = Gdx.graphics.getHeight();
setScreen(new SplashScreen(this));
}
@Override
public void render() {
super.render();
}
}
Here's my code for SplashScreen:
package com.jack.mygame.splash;
import com.badlogic.gdx.Gdx;
import static com.badlogic.gdx.Gdx.gl;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.jack.mygame.MyGame;
import com.jack.MyGame.screens.MainMenu;
public class SplashScreen implements Screen {
private Texture logo;
private SpriteBatch spriteBatch;
private MyGame game;
public SplashScreen(MyGame game) {
this.game = game;
}
@Override
public void show() {
logo = new Texture(Gdx.files.internal("logo.png"));
spriteBatch = new SpriteBatch();
}
@Override
public void render(float f) {
handleInput();
GL20 gl = Gdx.graphics.getGL20();
gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
spriteBatch.begin();
spriteBatch.draw(logo, 0, 0, MyGame.nWidth, MyGame.nHeight);
spriteBatch.end();
}
private void handleInput() {
if (Gdx.input.justTouched()) {
System.out.println("clicked");
game.setScreen(new MainMenu(game));
}
}
@Override
public void resize(int i, int i1) {
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void hide() {
}
@Override
public void dispose() {
}
}
Lastly, my code for the MainMenu:
package com.jack.mygame.screens;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Screen;
import com.jack.mygame.MyGame;
public class MainMenu implements Screen {
private Game game;
public MainMenu(Game game) {
this.game = game;
}
@Override
public void show() {
}
@Override
public void render(float f) {
}
@Override
public void resize(int i, int i1) {
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void hide() {
}
@Override
public void dispose() {
}
}
Any help would be amazing. Haven't messed with screens in LibGDX before.
Upvotes: 0
Views: 313
Reputation: 367
I tested this code and for me it works. The only problem is that your MainMenu class does not render any view. So when you click splashscreen you are not refreshing what is displayed on screen. Add for example this code in MainMenu class to clear screen from your splashscreen logo:
@Override
public void render(float f) {
GL20 gl = Gdx.graphics.getGL20();
gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
}
Upvotes: 1