2017-01-15 77 views
1

當我運行我的遊戲並單擊屏幕退出我的啓動畫面時,它應該將我帶到MainMenu中,但它會凍結。我找到了錯誤告訴我的代碼行,但它沒有幫助。(MainMenu.java:124)該行只顯示了stage.act();比stage.draw();如果我試圖刪除或註釋掉stage.act();。(MyGdxGame.java:31)然後指向我的super.render();.我不知道什麼是錯,任何幫助都會很好。stage.act()上的NullPointerException

錯誤:

Exception in thread "LWJGL Application" java.lang.NullPointerException 
    at com.jack.mygdxgame.screens.MainMenu.render(MainMenu.java:124) 
    at com.badlogic.gdx.Game.render(Game.java:46) 
    at com.jack.mygdxgame.MyGdxGame.render(MyGdxGame.java:31) 
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:225) 
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:126) 

MyGdxGame代碼:

package com.jack.mygdxgame; 

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.mygdxgame.screens.GameScreen; 
import com.jack.mygdxgame.splash.SplashScreen; 

public class MyGdxGame extends Game { 

    public static int nWidth; 
    public static int nHeight; 
    private Game game; 

    public MyGdxGame() { 
     game = this; 
    } 

    @Override 
    public void create() { 
     nWidth = Gdx.graphics.getWidth(); 
     nHeight = Gdx.graphics.getHeight(); 

     setScreen(new SplashScreen(this)); 
    } 

    @Override 
    public void render() { 
     super.render(); 
    } 
} 

的MainMenu代碼:

package com.jack.mygdxgame.screens; 

import com.badlogic.gdx.Game; 
import com.badlogic.gdx.Gdx; 
import com.badlogic.gdx.Screen; 
import com.badlogic.gdx.graphics.GL20; 
import com.jack.mygdxgame.MyGdxGame; 
import com.badlogic.gdx.ApplicationAdapter; 
import com.badlogic.gdx.Gdx; 
import com.badlogic.gdx.graphics.Color; 
import com.badlogic.gdx.graphics.GL20; 
import com.badlogic.gdx.graphics.Pixmap; 
import static com.badlogic.gdx.graphics.Pixmap.Format.RGB888; 
import com.badlogic.gdx.graphics.Texture; 
import com.badlogic.gdx.graphics.g2d.BitmapFont; 
import com.badlogic.gdx.scenes.scene2d.InputEvent; 
import com.badlogic.gdx.scenes.scene2d.Stage; 
import com.badlogic.gdx.scenes.scene2d.ui.TextButton; 
import com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle; 
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener; 
import com.badlogic.gdx.scenes.scene2d.ui.Skin; 

public class MainMenu implements Screen { 

    Stage stage; 
    Skin skin; 
    private Game game; 

    public MainMenu(Game game) { 
     this.game = game; 
    } 

    public void create() { 
     // load assets 

     int buttonOffSet = 20; 

     stage = new Stage(); 
     Gdx.input.setInputProcessor(stage);// Make the stage consume events 

     createBasicSkin(); 
     TextButton newGameButton = new TextButton("Start game", (com.badlogic.gdx.scenes.scene2d.ui.Skin) skin); // Use the initialized skin 
     newGameButton.setPosition(Gdx.graphics.getWidth()/2 - Gdx.graphics.getWidth()/8, Gdx.graphics.getHeight()/2 + (newGameButton.getHeight() + buttonOffSet)); 
     newGameButton.addListener(new ClickListener() { 

      @Override 
      public void clicked(InputEvent event, float x, float y) { 
       System.out.println("Start game button clicked"); 
      } 
     }); 

     stage.addActor(newGameButton); 

     TextButton loadButton = new TextButton("Load game", (com.badlogic.gdx.scenes.scene2d.ui.Skin) skin); // Use the initialized skin 
     loadButton.setPosition(Gdx.graphics.getWidth()/2 - Gdx.graphics.getWidth()/8, Gdx.graphics.getHeight()/2); 
     loadButton.addListener(new ClickListener() { 
      @Override 
      public void clicked(InputEvent event, float x, float y) { 
       System.out.println("Load Button clicked"); 
      } 
     }); 

     stage.addActor(loadButton); 

     TextButton settingsButton = new TextButton("Settings", (com.badlogic.gdx.scenes.scene2d.ui.Skin) skin); 
     settingsButton.setPosition(Gdx.graphics.getWidth()/2 - Gdx.graphics.getWidth()/8, Gdx.graphics.getHeight()/2 - (settingsButton.getHeight() + buttonOffSet)); 
     settingsButton.addListener(new ClickListener() { 
      @Override 
      public void clicked(InputEvent event, float x, float y) { 
       System.out.println("Settinga Button clicked"); 
      } 

     }); 

     stage.addActor(settingsButton); 

     TextButton exitGameButton = new TextButton("Exit game", (com.badlogic.gdx.scenes.scene2d.ui.Skin) skin); 
     exitGameButton.setPosition(Gdx.graphics.getWidth()/2 - Gdx.graphics.getWidth()/8, Gdx.graphics.getHeight()/2 - 2 * (newGameButton.getHeight() + buttonOffSet)); 
     exitGameButton.addListener(new ClickListener() { 
      @Override 
      public void clicked(InputEvent event, float x, float y) { 
       System.out.println("Exit game button clicked"); 
       System.exit(0); 
      } 
     }); 

     stage.addActor(exitGameButton); 
    } 

    private void createBasicSkin() { 
//Create a font 
     BitmapFont font = new BitmapFont(); 
     skin = new Skin(); 
     skin.add("default", font); 

     //Create a texture 
     Pixmap pixmap = new Pixmap((int) Gdx.graphics.getWidth()/4, (int) Gdx.graphics.getHeight()/10, Pixmap.Format.RGB888); 
     pixmap.setColor(Color.WHITE); 
     pixmap.fill(); 
     skin.add("background", new Texture(pixmap)); 

     //Create a button style 
     TextButton.TextButtonStyle textButtonStyle = new TextButton.TextButtonStyle(); 
     textButtonStyle.up = skin.newDrawable("background", Color.GRAY); 
     textButtonStyle.down = skin.newDrawable("background", Color.DARK_GRAY); 
     textButtonStyle.checked = skin.newDrawable("background", Color.DARK_GRAY); 
     textButtonStyle.over = skin.newDrawable("background", Color.LIGHT_GRAY); 
     textButtonStyle.font = skin.getFont("default"); 
     skin.add("default", textButtonStyle); 

    } 

    @Override 
    public void show() { 
    } 

    @Override 
    public void render(float f) { 
     GL20 gl = Gdx.graphics.getGL20(); 
     gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 

     Gdx.gl.glClearColor(1, 1, 1, 1); 

     stage.act(); 
     stage.draw(); 
    } 

    @Override 
    public void resize(int i, int i1) { 
    } 

    @Override 
    public void pause() { 
    } 

    @Override 
    public void resume() { 
    } 

    @Override 
    public void hide() { 
    } 

    @Override 
    public void dispose() { 
    } 

} 
+0

當您初始化皮膚時,可能出現錯誤,請嘗試將其從演員中刪除並告訴我。 – Mehdi

+0

splashscreen的代碼? – Aryan

+0

@Mehdi你是什麼意思?當我嘗試去除皮膚皮膚時;從我的代碼我只是得到更多的錯誤。 – Yeah

回答

2

當我想你從來沒有所謂的創造的MainMenu屏幕的方法。所以階段沒有初始化並拋出NullPointer異常。

使用show方法初始化屏幕變量。

+0

對不起,你是什麼意思?我在哪裏添加Show Method? – Yeah

+0

當屏幕成爲遊戲的當前屏幕時調用show()方法。屏幕界面有show()方法。您已經覆蓋此方法,但是這是空白的。所以把你的create()方法的代碼放在show()方法中。 – Aryan

+1

非常感謝你的工作! – Yeah