ExtendedSimpleApp - UnluckyNinja/Mouse-Killer GitHub Wiki
In this tutorial we will be extending the simple game "Drop", made in a previous tutorial. We will be adding a menu screen, and a couple of features to make this game a little more fully featured.
在这个教程中,我们将拓展上一个教程中制作的简单游戏“Drop”。我们将添加一个菜单窗口,以及一些特征,来使这个游戏更有一点“游戏”的味道。
Lets get started with an introduction to a few more advanced classes in our game.
让我们从介绍我们即将用到的一些高级的类开始。
Screens are fundamental to any game with multiple components. Screens contain much of the methods you are used to from ApplicationListener objects, and includes a couple new methods show and hide, which are called when the Screen gains and loses focus, respectively.
屏幕 是多组件游戏的基础。屏幕包含了大多数那些来自 ApplicationListener 的,你所熟悉的方法,还增加了一些新的方法如 show 和 hide,当窗口获得或失去焦点时分别被调用。 The Game abstract class provides an implementation of ApplicationListener for you to use, along with some helper methods to set and handle Screen rendering.
游戏 抽象类已实现了 ApplicationListener 接口,因此你可以直接使用它启动程序,还添加了一些帮助方法用于处理屏幕渲染。
Together, Screen and Game objects are used to create a simple and powerful structure for games.
结合到一起,屏幕 和游戏 对象被用来创建一个简单而有力的游戏架构。
We will start with creating a Game object, which will be the entry point to our game.
我们将从创建一个游戏 对象开始,这将是我们游戏的入口。
Lets show some code and walk through it:
让我们来浏览并分析一段代码:
package com.badlogic.drop;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class Drop extends Game {
SpriteBatch batch;
BitmapFont font;
public void create() {
batch = new SpriteBatch();
//使用 LibGDX 的默认 Arial 字体。
font = new BitmapFont();
this.setScreen(new MainMenuScreen(this));
}
public void render() {
super.render(); //重要!
}
public void dispose() {
batch.dispose();
font.dispose();
}
}
我们从初始化一个 SpriteBatch(画具)和 BitmapFont(位图字)对象来着手这个应用。能重复利用对象却仍然反复创建,这是个坏习惯(浏览 莫冗余)。 画具 对象是用来将材质等图像绘制到屏幕上的,而位图字 对象是通过画具 对象将文本绘制到屏幕上。我们将在屏幕 类中进一步了解这方面内容。
then, we set the Screen of the Game to a MainMenuScreen object, with a Drop instance as its first and only parameter.
然后,我们把游戏 的屏幕 做成一个 主菜单MainMenuScreen 对象,用 Drop 实例作为唯一参数。
A common mistake is to forget to call super.render() with a Game implementation. Without this call, the Screen that you set in the create() method will not be rendered!
一个常见的错误是,忘记在游戏 实现类中的render()方法中调用父方法。不调用的话,在 create()方法里设置的屏幕对象将不会被渲染!(源码)
public abstract class Game implements ApplicationListener {
...
public void render () {
if (screen != null) screen.render(Gdx.graphics.getDeltaTime());
}
...
}
最后,另一个关于处理对象的提示:深入阅读