无法居中图像scene2d

时间:2014-12-09 02:31:41

标签: java image scene2d

我试图以我的形象为中心,但我不能。我用视口尝试了一切,没有任何东西。我不想用魔法数字来制作技巧!默认视口可能搞乱了吗?

这是我的代码:

public class LoadingScreen extends AbstractScreen{

private Stage stage;
private Texture texture;
private Image loading_image;

public LoadingScreen(XBallGame game ) {
    super(game);
    texture = TextureManager.SPLASH;
    loading_image = new Image();
    stage = new Stage();
    }

@Override
public void render(float delta) {
    Gdx.gl.glClearColor(0,0,0,1); //sets clear color to black
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); //clear the batch
    stage.act(delta);
    stage.draw();
        }


@Override
public void show() {


loading_image.addAction(Actions.sequence(Actions.alpha(0)
        ,Actions.fadeIn(0.5f),Actions.delay(2),Actions.run(new Runnable() {
            @Override
                public void run() {
                    System.out.println("DONE");
                }
            })));

    stage.addActor(loading_image);
    loading_image.setPosition(Gdx.graphics.getWidth()/2, Gdx.graphics.getHeight()/2);
}


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

}

更新(修复了问题)

刚刚添加了这一行:

loading_image.setPosition((Gdx.graphics.getWidth()/2) - loading_image.getWidth()/2,
            (Gdx.graphics.getHeight()/2) - loading_image.getHeight()/2);

并且工作正常!

0 个答案:

没有答案