了解libgdx [英] Understanding libgdx

查看:134
本文介绍了了解libgdx的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,这是一个框架;更是开源的跨平台游戏开发库。我去 libgdx主页和遵循的视频教程的指令。在正确设置我的项目,我能够多支持的平台上运行默认我-GDX-游戏项目。太好了,好得很......现在该怎么办?

我一直在淘论坛,维基的javadoc,和许多许多的网站寻找体面的简单的如何做的。不幸的是,我找不到任何,大多数的帮助那里假设你有这个库的一些基本知识。

我觉得像视频教程,教我如何正确设置项目,有效地让我的脚湿,然后就认为我知道如何游泳,给我留下300英里入海什么的。我无法消化的图书馆,因为我才开始使用它昨天,所以我是一个完整的新手,当谈到libgdx。

我想将我现有的项目交给libgdx,但我使用到BufferedImages,JFrames之类的东西。从经验丰富的老兵任何帮助将是很好。

通过我下面贴我的核心项目的方式,这样你们可以通过走我究竟是什么回事?

 < code取代;
包com.me.mygdxgame;

进口com.badlogic.gdx.ApplicationListener;
进口com.badlogic.gdx.Gdx;
进口com.badlogic.gdx.graphics.GL20;
进口com.badlogic.gdx.graphics.OrthographicCamera;
进口com.badlogic.gdx.graphics.Texture;
进口com.badlogic.gdx.graphics.Texture.TextureFilter;
进口com.badlogic.gdx.graphics.g2d.Sprite;
进口com.badlogic.gdx.graphics.g2d.SpriteBatch;
进口com.badlogic.gdx.graphics.g2d.TextureRegion;

公共类MyGdxGame实现了ApplicationListener {
    私人OrthographicCamera摄像头;
    私人SpriteBatch批;
    私人纹理质感;
    私人雪碧雪碧;

    @覆盖
    公共无效创建(){

        浮瓦特= Gdx.graphics.getWidth();
        浮动H = Gdx.graphics.getHeight();

        相机=新OrthographicCamera(1,H / W);
        一批=新SpriteBatch();

        纹理=新的纹理(Gdx.files.internal(数据/ libgdx.png));
        texture.setFilter(TextureFilter.Linear,​​TextureFilter.Linear);

        TextureRegion区域=新TextureRegion(纹理,0,0,512,275);

        精灵=新的Sprite(区);
        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);

        }

    @覆盖
    公共无效的Dispose(){
        batch.dispose();
        texture.dispose();
    }

    @覆盖
    公共无效渲染(){
        Gdx.gl.glClearColor(1,1,1,1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        batch.setProjectionMatrix(camera.combined);
        batch.begin();
        sprite.draw(批);
        batch.end();
    }

    @覆盖
    公共无效调整大小(INT宽度,高度INT){
    }

    @覆盖
    公共无效暂停(){
    }

    @覆盖
    公共无效简历(){
    }
}
< / code取代;
 

解决方案

如果你想将你的的javax.swing 游戏libgdx你必须知道你是怎样使你游戏。你有没有分开的逻辑和观点?如果是的话,罚你只需要重写视图。如果不是,那么,它也许不如从头开始。 一是基础知识要知道:入口点到你的游戏是了ApplicationListener 接口。如果你不需要它里面的每一个方法,延长游戏,它给你一些deffault behaivor。所以我建议使用的。该游戏了ApplicationListener 现在给您可能对一些应用程序事件的反应:

  1. 创建():调用时您的应用程序已启动
  2. 配置():调用时你的游戏被关闭(不崩溃:P)
  3. 暂停():呼吁在Android上,当呼叫进入/ home键是pressed。就当你最小化窗口/当比赛失去焦点桌面。
  4. 简历():当你经过暂停状态回来了名为
  5. 调整():当你调整窗口的大小,并在Android上,调用时屏幕旋转(你必须允许它在manifest文件)
  6. 渲染():叫每gameloop(如果启用),用于更新在游戏中的对象,然后将它们画到屏幕
游戏类卡列斯

默认渲染()实现使您的电流屏幕

画面?那是什么? 那么想想一场普通的比赛。你开始它,它showes一个菜单,包含开始游戏设置一些按钮 ...这是菜单屏幕。因此,屏幕是不同的逻辑和观点你的游戏的一部分,使他们必须在sepparate类。 屏幕试行的屏幕接口,这又为您提供了一些有用的方法:

  1. 显示():叫,当你设置此屏幕游戏取值屏幕 MyGdxGame.setScreen()
  2. 隐藏():叫,当另一个屏幕设置为游戏取值屏幕

因此​​,如果当前的屏幕 MenuScreen 和我preSS的玩游戏按钮,显示()播放屏幕被称为和隐藏() MenuScreen 被调用。 该屏幕也有方法暂停()简历()调整(),被称为如果游戏相同的方法称为(永远只有当前屏幕)。如果实施了ApplicationListener 您必须编写此默认behaivor自己。
请注意,配置()屏幕不会自动调用。

接下来的事情就是绘画。对于2D绘图还有的:

  1. ShapeRenderer ,大多数时候只能用于调试。它可以使简单的形状,如圆形,方形...
  2. SpriteBatch ,用于呈现纹理取值雪碧第..

所以大多数时候,你会令使用 SpriteBatch 。您呈现由块:

  1. SpriteBatch.begin(),启动SpriteBatch。此调用之前设置了他的口中,矩阵...
  2. SpriteBatch.draw(),吸引你给它作为参数的东西(有很多不同的draw()方法,看看API)来缓冲,并的flush()他们到GPU尽快已满或 SpriteBatch.end()被调用。
  3. SpriteBatch.end()的flush()缓冲区的GPU,所以一切都画上了屏幕。

注:

  1. 从来没有2 SpriteBatches在他们的开始状态一次。所以在开始()荷兰国际集团一个SpriteBatch调用端()对其他正在运行的SpriteBatch
  2. 如果可以只使用1 SpriteBatch,并尝试做呼叫端()每个渲染循环只有一次,因为它的成本一些性能。
  3. 请不要叫画()的开始端块外。

最后但并非最不重要的这个指南: 该相机。它可以帮助你计算你的运动......在你自己的worldunits,而不是像素。你也可以使用它通过移动它角落找寻,以显示你的游戏世界的不同部分。 第一:设置在construcotr你camerasviewport:

 相机=新OrthographicCamera(16,9);
 

这台16宽9高度的视口。所以,你的物理屏幕/显示器现在16个单位的宽度和9个单位唤起注意。若要将此相机和SpriteBatch电话:

 `SpriteBatch.setProjectionMatrix(cam.combined)`
 

这样做的 SpriteBatch 渲染你的相机正在考虑的事情。 通过deffault摄像机0,0点是在它的中间。所以画的东西在中间现在使用:

  batch.draw(yourTexture,0,0);
 

要在画的右上角使用:

  batch.draw(yourTexture,16/2,9/2);
 

要移动您的相机使用的:

  cam.translate(16/2,9/2);
 

请确保调用 cam.update()当你改变了自己的立场!

现在相机的右上角是16.9,而不是16/2,9/2。而0,0点左下角。

我希望这是不复杂的起始。一些教程,它帮助我学习:

  1. <一个href="http://obviam.net/index.php/getting-started-in-android-game-development-with-libgdx-create-a-working-prototype-in-a-day-tutorial-part-1/"相对=nofollow> StarAssault
  2. Steigert的博客

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?

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.

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.

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>

解决方案

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:

  1. create(): called when your app is started
  2. dispose(): called when your game is closed (not crashed :P)
  3. pause(): called on android, when call is incoming/home button is pressed. On desktop when you minimize window/when the game lost the focus.
  4. resume(): called when you come back after pause state.
  5. resize(): called when you resize the window and on android, when screen rotates (you have to allow it in the manifest file)
  6. render(): called every gameloop (if enabled), used to update the objects in game and then draw them to the screen.

The default render() implementation in the Game class calles render for your current 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 Screens are parts of your game with different logic and view, so that they have to be in sepparate classes. Screens implement the Screen interface, which again gives you some useful methods:

  1. show(): called, when you set this Screen as the Games Screen with MyGdxGame.setScreen().
  2. hide(): called, when another Screen is set as the Games Screen.

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:

  1. ShapeRenderer, most times used for debug only. It can render simple shapes like circles, squares...
  2. SpriteBatch, used to render Textures Sprites...

So most times you will render using the SpriteBatch. You render in a block made of:

  1. SpriteBatch.begin(), starts the SpriteBatch. Set his viewPort, Matrices... before this call
  2. SpriteBatch.draw(), draws the thing you give it as parameter (there are many different draw() methods, look at the API) to a Buffer, and flush() them to the GPU as soon as it is full or SpriteBatch.end() is called.
  3. SpriteBatch.end(), flush() the buffer to the GPU, so everything is drawn on the screen.

Notes:

  1. Never have 2 SpriteBatches in their begin state at once. So before begin()ing a SpriteBatch call end() on the other running SpriteBatch
  2. If possible use only 1 SpriteBatch and try do call end() only once per render loop, as it cost some performance.
  3. Don't call draw() outside the begin-end block.

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);

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)`

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);

Make sure to call cam.update() after you change its position!

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:

  1. StarAssault
  2. Steigert's blog

这篇关于了解libgdx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆