libgdx 的整体架构如下:
入口:Game-->screen-->stage-->actor
具体如图:
因此我们需要分析下Game.java 和Screen.java 了。
game类位置为:
libgdx-master\gdx\src\com\badlogic\gdx
内容为:
public abstract class Game implements ApplicationListener {
private Screen screen;
@Override
public void dispose () {
if (screen != null) screen.hide();
}
@Override
public void pause () {
if (screen != null) screen.pause();
}
@Override
public void resume () {
if (screen != null) screen.resume();
}
@Override
public void render () {
if (screen != null) screen.render(Gdx.graphics.getDeltaTime());
}
@Override
public void resize (int width, int height) {
if (screen != null) screen.resize(width, height);
}
/** Sets the current screen. {@link Screen#hide()} is called on any old screen, and {@link Screen#show()} is called on the new
* screen, if any.
* @param screen may be {@code null} */
public void setScreen (Screen screen) {
if (this.screen != null) this.screen.hide();
this.screen = screen;
if (this.screen != null) {
this.screen.show();
this.screen.resize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
}
}
/** @return the currently active {@link Screen}. */
public Screen getScreen () {
return screen;
}
}
我们可以看到继承了ApplicationListener,因此他可以作为渲染对象增加到androidApplication的初始化接口里面,作为主线程的渲染对象。
它的各个接口处理了screen的生命周期。
主要来看看setScreen 和render 接口。
setScreen 完成将屏幕添加进来,以便后面其它接口调用。
render 则直接调用screen.render(Gdx.graphics.getDeltaTime()); 传递下去。
Screen对象代码为:
public interface Screen {
/** Called when the screen should render itself.
* @param delta The time in seconds since the last render. */
public void render (float delta);
/** @see ApplicationListener#resize(int, int) */
public void resize (int width, int height);
/** Called when this screen becomes the current screen for a {@link Game}. */
public void show ();
/** Called when this screen is no longer the current screen for a {@link Game}. */
public void hide ();
/** @see ApplicationListener#pause() */
public void pause ();
/** @see ApplicationListener#resume() */
public void resume ();
/** Called when this screen should release all resources. */
public void dispose ();
}
如此我们继承Screen实现我们的接口,添加进Game类里面,即可实现多个屏幕的切换。