2014-07-15 56 views
0

我試圖創建一個將某些文本繪製到屏幕的Actor。該演員在屏幕內。我的主類是簡單的:當我運行該程序的遊戲剛剛崩潰在Libgdx的屏幕內爲場景繪製字體

package com.culture.jamiefearon; 
import com.badlogic.gdx.Gdx; 
import com.badlogic.gdx.Screen; 
import com.badlogic.gdx.graphics.GL20; 
import com.badlogic.gdx.graphics.OrthographicCamera; 
import com.badlogic.gdx.scenes.scene2d.Actor; 
import com.badlogic.gdx.scenes.scene2d.Stage; 
import com.badlogic.gdx.graphics.g2d.SpriteBatch; 

public class IntroScreen implements Screen { 

final Culture game; 
OrthographicCamera camera; 
private int viewWidth = 800; 
private int viewHeight = 480; 
private Stage stage; 

public class MyActor extends Actor { 
    public void draw(SpriteBatch batch, float parentAlpha){ 
     batch = game.batch; 
     game.font.draw(batch, "Culture", viewWidth/2, viewHeight/2); 
    } 
} 

public void create() {   
    stage = new Stage(); 
    MyActor myActor = new MyActor(); 
    stage.addActor(myActor); 
} 


public IntroScreen(final Culture gam) { 
    game = gam; 
    camera = new OrthographicCamera(); 
    camera.setToOrtho(false, viewWidth, viewHeight); 
} 

public void render(float delta) { 
    Gdx.gl.glClearColor(0, 0, 0.2f, 1); 
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 

    camera.update(); 
    game.batch.setProjectionMatrix(camera.combined); 
    stage.draw(); 
} 

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

@Override 
public void show() { 

} 

@Override 
public void hide() { 
} 

@Override 
public void pause() { 
} 

@Override 
public void resume() { 
} 

@Override 
public void dispose() { 

} 
} 

package com.culture.jamiefearon; 
import com.badlogic.gdx.Game; 
import com.badlogic.gdx.graphics.g2d.BitmapFont; 
import com.badlogic.gdx.graphics.g2d.SpriteBatch; 

public class Culture extends Game { 
    public SpriteBatch batch; 
    public BitmapFont font; 

    public void create() { 
     batch = new SpriteBatch(); 
     font = new BitmapFont(); // Use LibGDX's default Arial font. 

     // Start the introScreen. 
     this.setScreen(new IntroScreen(this)); 
    } 
    public void render() { 
     super.render(); 
    } 
    public void dispose() { 
     batch.dispose(); 
     font.dispose(); 
    } 

}

而且我的屏幕類定義爲。

+0

是否有錯誤訊息?如果是這樣,你可以在這裏發佈嗎? – clearlyspam23

回答

0

您實例化了一個沒有SpriteBatch的舞臺,所以它創建了它自己的。然後你的MyActor忽略那個SpriteBatch並且試圖使用game中的一個,它沒有調用begin()

相反,您的舞臺應該使用來自game的SpriteBatch實例化,而MyActor應該只使用傳遞到其繪製方法的batch

這可能不是你得到的崩潰,但你沒有發佈你的堆棧跟蹤。

0

IntroScreen的創建方法從不調用,因此stage並未初始化並用於IntroScreen類的渲染方法。