简体   繁体   English

LibGDX:Screen接口与Game类的关系

[英]LibGDX: The relationship between Screen interface and Game class

I want to understand the relationship between the Game class and the Screen Interface.我想了解游戏类和屏幕界面之间的关系。 In the textbook I am reading, it states that the Game class delegates the functions from ApplicationListener to a screen object.在我正在阅读的教科书中,它指出 Game 类将 ApplicationListener 中的函数委托给一个屏幕对象。 And somehow this allows you to switch screens.不知何故,这允许您切换屏幕。 I don't get it, why does this happen and how does it work?我不明白,为什么会发生这种情况以及它是如何工作的?

My current understanding: Game implements Life Cycle methods from ApplicationListener(Create(), Render(), etc...).我目前的理解:游戏从 ApplicationListener(Create(), Render(), etc...) 实现生命周期方法。 But the reason you don't just implement from ApplicationListener directly is because the Game class will allow you to make multiple screens if you extend from the Game class instead.但是你不直接从 ApplicationListener 实现的原因是因为如果你从 Game 类扩展,Game 类将允许你制作多个屏幕。

The "core" of LibGDX is always your ApplicationListener class. LibGDX 的“核心”始终是您的ApplicationListener类。
It contains all the Lifecycle-Hooks the different platforms offer (including create , dispose and so on).它包含不同平台提供的所有 Lifecycle-Hooks(包括createdispose等)。
The class Game is just one implementation of the ApplicationListener , containing the most usual behavior. Game类只是ApplicationListener一种实现,包含最常见的行为。 For most of the games this class does a great job, if you need some speicial behavior, you need to override it or implement ApplicationListener yourself.对于大多数游戏,这个类做得很好,如果你需要一些特殊的行为,你需要覆盖它或自己实现ApplicationListener
The Screen interface isn't as important, but it is also pretty usefull. Screen界面并不重要,但它也非常有用。
It allowes you to separate your game into different parts.它允许您将游戏分成不同的部分。
For example you could have a MenuScreen and a GameScreen .例如,您可以有一个MenuScreen和一个GameScreen
The MenuScreen shows a simple menu with "Settings", "Highscores" and ofc a "Play"-Button. MenuScreen显示了一个带有“设置”、“高分”和“播放”按钮的简单菜单。
The GameScreen then contains the actualy game-logic and rendering. GameScreen然后包含实际的游戏逻辑和渲染。
However, by default Screen s don't do anything, they don't even get notified about the Lifecycle-Hooks.但是,默认情况下Screen不做任何事情,它们甚至不会收到有关 Lifecycle-Hooks 的通知。
And thats where the Game -class comes in:这就是Game类的用武之地:
The Game -class contains a single Screen -instance, the active Screen . Game类包含一个Screen实例,即活动Screen It then notifies the current Screen about Lifecycle-Events, like render .然后它会通知当前Screen有关 Lifecycle-Events 的信息,例如render The Screen can then take car about this event.然后, Screen可以了解此事件。
If you want to switch Screen , you can simply call Game.setScreen(newScreen) .如果你想切换Screen ,你可以简单地调用Game.setScreen(newScreen) The Game -class then calls hide for the current Screen (you might want to dispose some assets here, or save the users progress) and then show for the new Screen (here you can load some assets and initialize the new Screen ).然后Game类为当前Screen调用hide (您可能希望在此处处理一些资产,或保存用户进度),然后为新Screen show (在这里您可以加载一些资产并初始化新Screen )。

TL;DR TL; 博士
The ApplcationListener is the entry point of your game. ApplcationListener是游戏的入口点。 Each game has exactly one ApplicationListener , which gets notified about Lifecycle-Events by the LibGDX framework.每个游戏都有一个ApplicationListener ,它会通过 LibGDX 框架获得有关 Lifecycle-Events 的通知。

Screen s are different parts of your game, which contain different logik and view (for example a MenuScreen and the GameScreen ). Screen是游戏的不同部分,包含不同的逻辑和视图(例如MenuScreenGameScreen )。
The Screen -classes encapsulate the logic for a single Screen . Screen类封装了单个Screen的逻辑。

The Game -class is somehow the default implementation of the ApplicationListener interface and delegates most of the work to the current Screen . Game类在某种程度上是ApplicationListener接口的默认实现,并将大部分工作委托给当前Screen It also contains the logik for switching the Screen .它还包含用于切换Screen的逻辑。

ApplicationListener is just an Interface and you can implement it directly with your class. ApplicationListener 只是一个接口,你可以直接用你的类来实现它。 Game class is implementing that ApplicationListener Interface.游戏类正在实现该 ApplicationListener 接口。 Inside Game class it consists a Screen interface that will allow you to change Screens.在 Game 类中,它包含一个 Screen 接口,可让您更改屏幕。 Here is what inside the Game class fro Libgdx.这是 Libgdx 的 Game 类中的内容。

public abstract class Game implements ApplicationListener {
protected 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;
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM