在Libgdx中渲染图像

时间:2016-03-07 06:21:18

标签: java android libgdx scene2d

最近我开始使用Libgdx进行开发,遇到了一个我似乎无法解决的问题。我想将一个Image对象从Scene2d渲染到屏幕,但它不会渲染任何东西。它只是显示一个空白屏幕。

这是我的skin.json文件:

 {

com.badlogic.gdx.scenes.scene2d.ui.Image: {
    logo: { drawable: logo }
    exit: { drawable: exit }
},
com.badlogic.gdx.graphics.Color: {
    green: { a: 1, b: 0, g: 1, r: 0 },
    white: { a: 1, b: 1, g: 1, r: 1 },
    red: { a: 1, b: 0, g: 0, r: 1 },
    black: { a: 1, b: 0, g: 0, r: 0 },
},

}

这是我的skin.atlas文件:

skin.png
format: RGBA8888
filter: Nearest,Nearest
repeat: none
logo
  rotate: false
  xy: 1, 1
  size: 384, 93
  orig: 384, 93
  offset: 0, 0
  index: -1
help
  rotate: false
  xy: 387, 30
  size: 65, 64
  orig: 65, 64
  offset: 0, 0
  index: -1
musicoff
  rotate: false
  xy: 454, 30
  size: 65, 64
  orig: 65, 64
  offset: 0, 0
  index: -1
musicon
  rotate: false
  xy: 521, 30
  size: 65, 64
  orig: 65, 64
  offset: 0, 0
  index: -1
exit
  rotate: false
  xy: 588, 30
  size: 64, 64
  orig: 64, 64
  offset: 0, 0
  index: -1
settings
  rotate: false
  xy: 654, 30
  size: 64, 64
  orig: 64, 64
  offset: 0, 0
  index: -1

ScreenWrapper类:

package screens;

import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.utils.viewport.StretchViewport;

import engine.Values;

import com.badlogic.gdx.scenes.scene2d.Stage;

public abstract class ScreenWrapper extends Stage implements Screen{

    protected ScreenManager manager;
    public ScreenWrapper(ScreenManager manager) {
        super( new StretchViewport(Values.WIDTH, Values.HEIGHT, new OrthographicCamera()));
        this.manager = manager;
    }

    // All the actors will be added here
    public abstract void initiate();
    public abstract void buildStage();
    @Override
    public void show() {

    }
    @Override
    public void render(float delta) {
        act();
        draw();
    }
    @Override
    public void resize(int width, int height) {
        setViewport(new StretchViewport(Values.WIDTH, Values.HEIGHT));
    }
    @Override
    public void pause() {

    }
    @Override
    public void resume() {

    }
    @Override
    public void hide() {

    }
}

这是我的主要文件:

package screens;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;

import engine.Values;

public class MenuScreen extends ScreenWrapper{

    private ScreenRenderer renderer;
    public MenuScreen(ScreenManager manager) {
        super(manager);
        renderer = new ScreenRenderer();
    }

    @Override
    public void initiate() {
        manager.getLoader().load("skin.json", Skin.class);
    }
    @Override
    public void buildStage() {
        Skin skin = manager.getLoader().getAsset("skin.json", Skin.class);
        Image logo = new Image(skin.getDrawable("logo"));
        logo.setSize(Values.LOGO_WIDTH, Values.LOGO_HEIGHT);
        logo.setOrigin(logo.getX() + Values.LOGO_WIDTH / 2, logo.getY()
                + Values.LOGO_HEIGHT / 2);
        logo.setPosition(Values.WIDTH / 2 - Values.LOGO_WIDTH / 2,
                Values.HEIGHT);
        addActor(logo);
        Gdx.app.log("X and Y", "X : " + logo.getX() + ", Y : "+logo.getY());
        Gdx.app.log("Width and Height", "Width : " + logo.getWidth() + ", Height : " + Values.HEIGHT);
    }

    @Override
    public void render(float delta) {
        super.render(delta);
        Gdx.gl.glClearColor(242/255f, 242/255f, 242/255f, 0);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    }

}

我做错了什么?

编辑:好的,我将问题缩小到我的JSON文件。看起来我没有正确地声明徽标元素。如何在JSON中声明图像元素?

编辑2:我尝试将JSON文件更改为:

{

com.badlogic.gdx.scenes.scene2d.ui.ImageButton$ImageButtonStyle: {
    logo: { imageUp: logo, imageDown: logo }
},
com.badlogic.gdx.graphics.Color: {
    green: { a: 1, b: 0, g: 1, r: 0 },
    white: { a: 1, b: 1, g: 1, r: 1 },
    red: { a: 1, b: 0, g: 0, r: 1 },
    black: { a: 1, b: 0, g: 0, r: 0 },
},

}

但它仍然不起作用......

编辑3:我试图将徽标的.png文件作为Texture对象加载到徽标actor中,但它仍然没有绘制任何内容。我是这样做的:pastebin.com/n6iB2ymD

解决方案:好的,我终于弄明白了。事实证明,Values.Logo_Width和Values.Logo_Height为0,因此Image的大小设置为0 ...

2 个答案:

答案 0 :(得分:0)

MenuScreenStage吗?从代码中不太清楚。如果是,您需要在act方法中添加对drawrender方法的调用:

@Override
public void render(float delta) {
    super.render(delta);
    Gdx.gl.glClearColor(242/255f, 242/255f, 242/255f, 0);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    act();
    draw();
}

这些方法将Actors添加到舞台上。

答案 1 :(得分:0)

您需要确保在绘图之前清除屏幕,而不是之后。

@Override
public void render(float delta) {
    Gdx.gl.glClearColor(242/255f, 242/255f, 242/255f, 0);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    super.render(delta);
}

此外,您正在舞台上设置新视口而不更新它,因此视口不知道屏幕的大小。在ScreenWrapper中更改您的resize方法,如下所示:

@Override
public void resize(int width, int height) {
    getViewport().update(width, height);
}