了解 libgdx
我理解它是一个框架;更是一个开源的跨平台游戏开发库.我去了 libgdx 主页 并按照视频教程中的说明进行操作.正确设置我的项目后,我能够在多个支持的平台上运行默认的 my-gdx-game 项目.很好,很好,花花公子......现在呢?
I understand that it is a framework; even more an open-source cross-platform game development library. I went to the libgdx homepage and followed the instruction on the video tutorial. After properly setting up my project I was able to run the default my-gdx-game project on the multiple supported platforms. Great, fine and dandy...now what?
我一直在搜索论坛、wiki、javadocs 和许多其他网站,寻找体面的简单操作方法.不幸的是,我找不到任何帮助,那里的大多数帮助都假设您对该库有一些基本知识.
I have been scouring the forums, wikis, javadocs, and many many more sites looking for decent straightforward how-to's. Unfortunately I couldn't find any, most of the help out there assumes you have some basic knowledge of this library.
我觉得视频教程向我展示了如何正确设置项目,有效地弄湿我的脚,然后假设我知道如何游泳,然后让我离开了 300 英里的大海或其他地方.我无法消化这个库,因为我昨天才开始使用它,所以当谈到 libgdx 时,我是一个全新的人.
I feel like the video tutorial showed me how to set up the project correctly, effectively getting my feet wet, then just assumed I know how to swim, and left me 300 miles out into the sea or something. I'm having trouble digesting the library, because I only started using it yesterday, so I'm a complete newcomer when it comes to libgdx.
我想将我现有的项目转移到 libgdx,但我习惯于使用 BufferedImages、JFrames 和类似的东西.有经验的退伍军人的任何帮助都会很好.
I want to move my existing projects over to libgdx, but I'm use to BufferedImages, JFrames and things like that. Any help from the experienced veterans would be nice.
顺便说一下,我在下面发布了我的核心项目,所以你们可以带我了解这里到底发生了什么......
By the way I posted my core project below, so you guys can walk me through what is exactly going on here...
<code>
package com.me.mygdxgame;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
public class MyGdxGame implements ApplicationListener {
private OrthographicCamera camera;
private SpriteBatch batch;
private Texture texture;
private Sprite sprite;
@Override
public void create() {
float w = Gdx.graphics.getWidth();
float h = Gdx.graphics.getHeight();
camera = new OrthographicCamera(1, h/w);
batch = new SpriteBatch();
texture = new Texture(Gdx.files.internal("data/libgdx.png"));
texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
TextureRegion region = new TextureRegion(texture, 0, 0, 512, 275);
sprite = new Sprite(region);
sprite.setSize(0.9f, 0.9f * sprite.getHeight() / sprite.getWidth());
sprite.setOrigin(sprite.getWidth()/2, sprite.getHeight()/2);
sprite.setPosition(-sprite.getWidth()/2, -sprite.getHeight()/2);
}
@Override
public void dispose() {
batch.dispose();
texture.dispose();
}
@Override
public void render() {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(camera.combined);
batch.begin();
sprite.draw(batch);
batch.end();
}
@Override
public void resize(int width, int height) {
}
@Override
public void pause() {
}
@Override
public void resume() {
}
}
</code>
推荐答案
如果你想将你的 javax.swing
游戏移植到 libgdx,你必须知道你是如何制作你的游戏的.你把逻辑和视图分开了吗?如果是,那很好,你只需要重写视图.如果没有,那么,从头开始可能会更好.首先要了解的基础知识:游戏的入口点是 ApplicationListener
接口.如果您不需要其中的每个方法,请扩展 Game
,它会为您提供一些默认行为.所以我建议使用它.Game
或 ApplicationListener
现在让您可以对某些应用程序事件做出反应:
If you want to port your javax.swing
game to libgdx you have to know how you made your game. Did you separate the logic and the view? If yes, fine you only have to rewrite the view. If not, well, its maybe better to start from scratch.
First basics to know: The entrypoint to your game is the ApplicationListener
Interface. If you don't need every method inside it, extend Game
, which gives you some deffault behaivor. So i would suggest to use that. The Game
or ApplicationListener
now gives you possibility to react on some application events:
create()
:在应用启动时调用dispose()
:当你的游戏关闭时调用(不是崩溃:P)pause()
:在android上调用,当来电/按下home键时.当您最小化窗口/游戏失去焦点时在桌面上.resume()
:暂停状态后返回时调用.resize()
:当你调整窗口大小时调用render()
:调用每个游戏循环(如果启用),用于更新游戏中的对象,然后将它们绘制到屏幕上.
create()
: called when your app is starteddispose()
: called when your game is closed (not crashed :P)pause()
: called on android, when call is incoming/home button is pressed. On desktop when you minimize window/when the game lost the focus.resume()
: called when you come back after pause state.resize()
: called when you resize the window and on android, when screen rotates (you have to allow it in the manifest file)render()
: called every gameloop (if enabled), used to update the objects in game and then draw them to the screen.
Game
类中的默认 render() 实现会为您当前的 Screen
调用 render.
The default render() implementation in the Game
class calles render for your current Screen
.
屏幕?那是什么?好吧,想想一个正常的游戏.您启动它,它会显示一个菜单,其中包含一些按钮,例如 Start Game
、Settings
... 这是菜单屏幕.所以 Screen
是游戏的一部分,具有不同的逻辑和视图,因此它们必须位于单独的类中.Screen
实现了 Screen
接口,它再次为您提供了一些有用的方法:
Screen? Whats that?
Well think about a normal game. You start it and it showes a menu, with some buttons like Start Game
, Settings
... This is the Menu screen. So Screen
s are parts of your game with different logic and view, so that they have to be in sepparate classes. Screen
s implement the Screen
interface, which again gives you some useful methods:
show()
:当你将这个Screen
设置为Game
的Screen
和时调用>MyGdxGame.setScreen()
.hide()
:当另一个Screen
设置为Game
的Screen
时调用.李>
show()
: called, when you set thisScreen
as theGame
sScreen
withMyGdxGame.setScreen()
.hide()
: called, when anotherScreen
is set as theGame
sScreen
.
所以如果当前 Screen
是 MenuScreen
并且我按下 Play Game
按钮,show()
PlayScreen
的调用和 MenuScreen
的 hide()
调用.Screen
也有 pause()
、resume()
和 resize()
方法,如果Game
中的相同方法被调用(总是只针对当前屏幕).如果您实现 ApplicationListener
,则必须自己编写此默认行为.
请注意,Screen
的 dispose()
不会自动调用.
So if the current Screen
is the MenuScreen
and i press the Play Game
button, show()
for the PlayScreen
is called and hide()
for the MenuScreen
is called.
The Screen
also has the methods pause()
, resume()
and resize()
, which are called if the same methods in Game
are called (always only for the current Screen). If you implement ApplicationListener
you have to write this default behaivor yourself.
Note, that dispose()
for the Screen
is not called automatically.
接下来就是绘图了.对于二维绘图,有:
Next thing is the drawing. For drawing in 2d there are the:
ShapeRenderer
,大多数时候仅用于调试.它可以渲染简单的形状,如圆形、正方形...SpriteBatch
,用于渲染Texture
sSprite
s...
ShapeRenderer
, most times used for debug only. It can render simple shapes like circles, squares...SpriteBatch
, used to renderTexture
sSprite
s...
所以大多数时候您将使用 SpriteBatch
进行渲染.你在一个块中渲染:
So most times you will render using the SpriteBatch
. You render in a block made of:
SpriteBatch.begin()
,启动 SpriteBatch.在此调用之前设置他的视口、矩阵...SpriteBatch.draw()
,将你给它的东西作为参数(有很多不同的draw()方法,看看API)绘制到一个Buffer,然后flush()
在 GPU 满或调用SpriteBatch.end()
后立即将它们发送到 GPU.SpriteBatch.end()
,flush()
缓冲区到GPU,所以一切都在屏幕上绘制.
SpriteBatch.begin()
, starts the SpriteBatch. Set his viewPort, Matrices... before this callSpriteBatch.draw()
, draws the thing you give it as parameter (there are many different draw() methods, look at the API) to a Buffer, andflush()
them to the GPU as soon as it is full orSpriteBatch.end()
is called.SpriteBatch.end()
,flush()
the buffer to the GPU, so everything is drawn on the screen.
注意事项:
- 永远不要同时有 2 个 SpriteBatch 处于其
begin
状态.所以在begin()
调用 SpriteBatch 之前end()
在另一个正在运行的 SpriteBatch - 如果可能的话,只使用 1 个 SpriteBatch 并尝试在每个渲染循环中只调用一次
end()
,因为它会消耗一些性能. - 不要在 begin-end 块之外调用
draw()
.
- Never have 2 SpriteBatches in their
begin
state at once. So beforebegin()
ing a SpriteBatch callend()
on the other running SpriteBatch - If possible use only 1 SpriteBatch and try do call
end()
only once per render loop, as it cost some performance. - Don't call
draw()
outside the begin-end block.
最后但并非最不重要的教程":相机
.它可以帮助你计算你的动作......在你自己的世界单位而不是像素中.您还可以通过移动它来使用它来显示游戏世界的不同部分.第一:在constructor中设置你的cameraviewport:
Last but not least for this "Tutorial":
The camera
. It helps you calculating your movements... in your own worldunits instead of pixels. Also you can use it to show different parts of your gameworld by moving it arround.
First: set your camerasviewport in the construcotr:
camera = new OrthographicCamera(16, 9);
这会设置一个 16 宽和 9 高的视口.所以你的物理屏幕/显示器现在是 16 个单位宽和 9 个单位高.将此相机应用于您的 SpriteBatch 调用:
This sets a viewport of 16 width and 9 height. So your physical screen/monitor is now 16 units width and 9 units heigh. To apply this camera to your SpriteBatch call:
`SpriteBatch.setProjectionMatrix(cam.combined)`
通过这样做,SpriteBatch
会渲染您的相机正在查看的内容.默认情况下,摄像机 0,0 点位于其中间.所以现在要在中间画一些东西:
By doing this the SpriteBatch
renders the things your camera is looking at.
The cameras 0,0 point by deffault is in its middle. So to draw something in the middle now use:
batch.draw(yourTexture, 0, 0);
要在右上角绘制,请使用:
To draw in the upper right corner use:
batch.draw(yourTexture, 16 / 2, 9 / 2);
要移动您的相机,请使用:
To move your camera use:
cam.translate(16/2, 9/2);
确保在更改位置后调用 cam.update()
!
Make sure to call cam.update()
after you change its position!
现在您的相机右上角位于 16,9,而不是 16/2、9/2.而0,0点就是左下角.
Now the right upper corner of your camera is at 16,9 instead of 16/2, 9/2. And the 0,0 point is the left lower corner.
我希望这在一开始并不复杂.一些帮助我学习的教程:
I hope this was not to complicated for the begining. Some tutorials, which helped me to learn:
- StarAssault
- 施泰格的博客
相关文章