2

我设法使用本教程http://code.google.com/p/libgdx/wiki/IntelliJIDEALibgdx在 intellij idea 12 中设置 libgdx但我不确定当我运行看起来像这样的桌面应用程序时这是错误的。在此处输入图像描述

我期待 libgdx 的图像会像官方 libgdx 网站上的教程一样出现。对于教程的 intelliJ 部分,我将桌面链接到 android 的资产文件夹。如下图所示,是 android 模块的资产文件夹,其中包含 libgdx 的图像。

在此处输入图像描述

我遵循教程中的备选方案 2 。

这是代码:

DesktopStarter 类 - 桌面模块:

public class DesktopStarter {
    public static void main(String[] args) {
        LwjglApplicationConfiguration cfg = new LwjglApplicationConfiguration();
        cfg.title = "Title";
        cfg.useGL20 = true;
        cfg.width = 800;
        cfg.height = 480;
        new LwjglApplication(new MyLibgdxGame(), cfg);
    }
}

MyLibgdxGameAndroidStarter 类 - Android 模块:

public class MyLibgdxGameAndroidStarter extends AndroidApplication
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
        cfg.useAccelerometer = false;
        cfg.useCompass = false;
        cfg.useWakelock = true;
        cfg.useGL20 = true;
        initialize(new MyLibgdxGame(), cfg);
    }
}

MyLibgdxGame 类 - 主模块:

public class MyLibgdxGame extends Game {
    @Override
    public void create() {
        //To change body of implemented methods use File | Settings | File Templates.
    }
}

在此处下载项目

有谁知道出了什么问题?谢谢

4

1 回答 1

5

你的MyLibgdxGame类错过了显示图像的源代码,它应该如下所示:

package com.example.mylibgdxgame;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
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 MyLibgdxGame extends Game {
  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("libgdx2.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(GL10.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() {
  }
}

该指南建议使用gdx-setup-ui.jar它将生成带有此代码的完整示例应用程序。

结果如下:

样本

于 2013-03-03T21:11:51.493 回答