我试图在我的 GLSurefaceView.Renderer 类中设置我的初始游戏状态,并在 InitialGameState(Context) 中创建我的 Sprite 对象并在 render() 中调用它的方法,但是在 GLThread 中引发了异常。
public class MyRenderer implements GLSurfaceView.Renderer
{
private Context m_context;
private GameStateManager m_game;
public MyRenderer(Context context)
{
m_context = context;
m_game = GameStateManager.getInstance();
m_game.setGameState(new InGame(m_context));
}
@Override
public void onDrawFrame(GL10 gl)
{
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
m_game.getCurrentState().render();
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height)
{
GLES20.glViewport(0, 0, width, height);
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config)
{
GLES20.glClearColor(0.63671875f, 0.76953125f, 0.22265625f, 1.0f);
ShaderManager.getInstance().createDefaultShaders(m_context);
}
}
public class InGame extends GameState
{
private Sprite m_sprite;
private boolean m_isInitialized = false;
public InGame(Context context)
{
super(context);
m_sprite = new Sprite(m_context);
}
@Override
public void render()
{
if (!m_isInitialized)
{
m_sprite.create("clover.png");
m_isInitialized = true;
}
else
{
m_sprite.begin();
m_sprite.draw();
m_sprite.end();
}
}
}
当我在 onSurfaceCreated() 中调用 m_game.setGameState(new InGame(context)) 时,一切正常。这可以解决吗?
Thread [<9> GLThread 10] (Suspended (exception NullPointerException))
<VM does not provide monitor information>
GLSurfaceView$GLThread.run() line: 1122