简体   繁体   中英

Libgdx intellij Black screen application when run

I manage to set up libgdx in intellij idea 12 using this tutorial http://code.google.com/p/libgdx/wiki/IntelliJIDEALibgdx but im not sure wthat is wrong when i run the desktop applcation it looks like this. 在此处输入图片说明

I was expecting for an image of libgdx would appear just like the tutorial on official libgdx website . And for the intelliJ part of tutorial i link the desktop to asset folder of android. As you can see on the image below is the asset folder of android module which contain the image of libgdx.

在此处输入图片说明

I follow the Alternative 2 in the tutorial .

Here is the code:

DesktopStarter class - Desktop Module:

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 class - Android Module:

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 class - Main Module:

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

Download the project here

Anyone knows what went wrong? Thanks

Your MyLibgdxGame class misses the source code to display the image, it should look like this:

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 suggests to use gdx-setup-ui.jar that will generate a complete sample application with this code.

Here is the result:

样品

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM