[LibGdx] Screen transition

I’m trying to switch to another screen when the space key is pressed. Here is what my main game would look like.

I’m trying to switch to another screen via spacebar. For some reason, when I call the dispose method, it doesn’t do anything. My music is looped when I call play once. I’m not sure if things are disposed and It appears you need Scene2D to do any screen transition. Is there a simpler way?

public void create () {
//name of my first screen
		this.setScreen(new Title(this));
		
		
	}
	
	@Override
	public void render () {
		super.render();
		
	}
	
	public void dispose () {
		super.dispose();
		
	}

In the title screen whenever it updates it does this.

if (Gdx.input.isKeyPressed(Keys.SPACE)) {
//hide method disposes things which doesn't work. Nature is never paused and loops 
			hide();
			Nature.pause();
			hos.setScreen(new mainGame(hos));
			
			
		}

My first screen class: http://pastebin.java-gaming.org/371717b4e0913

You should clarify what your question or problem is. It takes a close reading of your post to figure it out.

The link to your Main Game class is broken.

Is the method checkGamestarted() called from somewhere?

–edit–

I see now it is called in update which is called from render

My bad, I was in a rush, but here is the main class. http://pastebin.java-gaming.org/7171b8e490311

It does not look like this code will compile. The ‘hos’ variable is not defined anywhere.

You probably should create just one instance of your GameScreen class when the app starts instead of instantiating it when the space bar is pressed so you don’t accidentally end up with more than one of them.

Don’t call super.render() or super.dispose(). I do not know what that does, maybe an infinite loop?

It looks like you end up calling Nature.dispose before calling Nature.pause in hide. That looks like it could be unsafe.

-edit-
You can change screens without Scene2D like how you are doing:
https://code.google.com/p/libgdx-users/wiki/ScreenAndGameClasses

Maybe it just looks like it’s not changing because you are not clearing the screen or drawing anything in the Game class?

Also call .setLooping(false) if you don’t want a Music instance to loop.

Perfect, exactly what I was looking for. When I was moving code around, I was very close. I need to practice better coding. :emo:

Everything has been working fine, but the music I play wont stop from a keypress no matter what I do.

Music Theme;
Theme.play();
		Theme.setVolume(0.1f);
		Theme.setLooping(true);
//When looping is false it still plays until it's finished
if (Gdx.input.isKeyPressed(Keys.A)) {
Theme.pause;
Theme.dispose;
//This is what's not working, in the update() it constantly checks if "A " is pressed
			main.setScreen(main.otherScreen);	
		}

Theme.pause;
Theme.dispose;

Those are supposed to be method calls right? This is obviously not real compiling code. How can anyone possibly help you with your problem in your code if you post this pseudocode.

Also please do not capitalize variable names like “Theme” it is very misleading when you are reading the code.

I’ll just have to show source code. It wont be too long, but here is it.

First off we have my Main Game class: http://pastebin.com/58z97DDY

Then The Title class it requires: http://pastebin.com/cUsttVqX

And Lastly, the second screen:
http://pastebin.com/C5aN4BZ5

In the title class, I’m trying to pause the music or stop it somehow.

Looks like you commented out the this:
//update(delta);
so it never calls update and thus your checkGamestarted method never gets called.

there are plenty of other problems that will keep this code from compiling anyway…

I call update in the main class

You need to put brackets around the body of those if statements or else the if only applies to the first statement following it:

            if(this.getScreen() == titleScreen) titleScreen.render(delta); titleScreen.update(delta);
           
            if(this.getScreen() == game) game.render(delta); game.update(delta);

do it like this:
if(this.getScreen() == titleScreen) {
titleScreen.render(delta);
titleScreen.update(delta);
}

            if(this.getScreen() == game) {
                game.render(delta);
                game.update(delta);
            }

And that’s why you should always use the auto-formatter.

Didn’t help :-\ music still never stops.

I noticed that I can make the music play on keypress, but whenever I have anything to do with switching screens in the same if statement, the music doesn’t do anything.

I didn’t figure out what the problem was, but since I didn’t do too much (3 Days :frowning: ) . Using this code, the music works just fine.

public class MainMenuScreen implements Screen {
	 

    HighOverSeas game; 
    Music a;


    public MainMenuScreen(HighOverSeas game){
             this.game = game;
             
     		a = Gdx.audio.newMusic(Gdx.files.internal("Audio/Music/test 3.wav"));
     		a.play();
     }
     
     @Override
     public void render(float delta) {
          if (Gdx.input.isKeyPressed(Keys.SPACE)){
        	  a.stop();
              game.setScreen(game.anotherScreen);
          System.out.println("switching to another");
        	  
          }
     }


    @Override
     public void resize(int width, int height) {
     }


    @Override
     public void show() {
          // called when this screen is set as the screen with game.setScreen();
     }


    @Override
     public void hide() {
          // called when current screen changes from this to a different screen
     }


    @Override
     public void pause() {
     }


    @Override
     public void resume() {
     }


    @Override
     public void dispose() {
             // never called automatically
     }
}