运行时Libgdx intellij黑屏应用程序

时间:2013-03-03 19:40:33

标签: java android intellij-idea libgdx

我设法使用本教程http://code.google.com/p/libgdx/wiki/IntelliJIDEALibgdx在intellij idea 12中设置libgdx但我不确定当我运行桌面应用程序时它是错误的,它看起来像这样。 enter image description here

我期待libgdx的图像就像official libgdx website上的教程一样。对于教程的intelliJ部分,我将桌面链接到android的资产文件夹。如下图所示,android模块的资产文件夹包含libgdx的图像。

enter image description here

我按照tutorial中的备选方案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.
    }
}

下载项目here

任何人都知道出了什么问题?感谢

1 个答案:

答案 0 :(得分: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() {
  }
}

The guide建议使用gdx-setup-ui.jar,使用此代码生成完整的示例应用程序。

结果如下:

sample