Libgdx屏幕渲染功能被调用但不是绘图

时间:2014-02-09 01:23:58

标签: java android libgdx

我在屏幕上的渲染时遇到了问题。

基本上我有一个扩展游戏的整体课程,我为我的游戏的各个页面创建了一些其他类,例如主菜单,实际游戏等。当我在其中一个类上调用setScreen(屏幕)时该屏幕的渲染循环被调用,但我似乎无法绘制任何东西。

我所做的是在整个游戏类中创建了正交相机和spograbatch,并通过构造方法将其传递给屏幕。但是,我似乎无法绘制任何东西。屏幕仍然清除背景。

抱歉,我目前没有源代码,但这里大概是这样的:

这是我的整体游戏课程:

public class MyGdxGame extends Game {

public OrthographicCamera camera;
public SpriteBatch batch;
public ResourceManager Rm;

public StartScreen MainMenu;
public GameScreen CellTD;
public InstructionsScreen Instructions;
public PauseScreen Pause;

@Override
public void create() {      
    float w = Gdx.graphics.getWidth();
    float h = Gdx.graphics.getHeight();

    Rm = new ResourceManager();
    camera = new OrthographicCamera(1.0f, h/w);
    batch = new SpriteBatch();

    Rm.LoadTexture("Cell.png");

    MainMenu = new StartScreen(camera, batch, Rm, this);
    CellTD = new GameScreen(camera, batch, Rm, this);
    Instructions = new InstructionsScreen(camera, batch, Rm, this);
    Pause = new PauseScreen(camera, batch, Rm, this);

    setScreen(MainMenu);

}

@Override
public void dispose() {
}

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

@Override
public void pause() {
}

@Override
public void resume() {
}

}

这是我的一个屏幕类:

public class StartScreen implements Screen{

private OrthographicCamera camera;
private SpriteBatch batch;
private ResourceManager Rm;
private int SCREEN_W, SCREEN_H;
public MyGdxGame Parent;
private Label Title;
private Sprite s;

StartScreen(OrthographicCamera c, SpriteBatch b, ResourceManager r, MyGdxGame g)
{
    camera = c;
    batch = b;
    Rm = r;
    SCREEN_W = Gdx.graphics.getWidth();
    SCREEN_H = Gdx.graphics.getHeight();
    Parent = g;

    camera.setToOrtho(false,1.0f,SCREEN_H/SCREEN_W);
    camera.update();

    Title = new Label("Cell TD",new Label.LabelStyle(new BitmapFont(Gdx.files.internal("data/CellTDFont.fnt"),false) ,new Color(1.0f,1.0f,1.0f,1.0f)));
    Title.setText("Cell TD");
    Title.setSize(1.0f, SCREEN_H/SCREEN_W);
    Title.setOrigin(Title.getWidth()/2, Title.getHeight()/2);
    Title.setPosition(0, 0);

    s = new Sprite(Rm.GetTexture("Cell.png"));
    s.setSize(1.0f, SCREEN_H/SCREEN_W);
    s.setOrigin(s.getWidth()/2, s.getHeight()/2);
    s.setPosition(-0.5f, -SCREEN_H/SCREEN_W/2);
}
@Override
public void render(float delta) {
    Gdx.gl.glClearColor(0, 0.9f, 0.9f, 0);
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

    batch.setProjectionMatrix(camera.combined);
    batch.begin();
    //Title.draw(batch,1);
    s.draw(batch);

    batch.end();

}

@Override
public void resize(int width, int height) {
    // TODO Auto-generated method stub

}

@Override
public void show() {
    // TODO Auto-generated method stub

}

@Override
public void hide() {
    // TODO Auto-generated method stub

}

@Override
public void pause() {
    // TODO Auto-generated method stub

}

@Override
public void resume() {
    // TODO Auto-generated method stub

}

@Override
public void dispose() {
    // TODO Auto-generated method stub

}

}

1 个答案:

答案 0 :(得分:0)

这是一个容易犯的错误 我想你的相机不正确。

c = new OrthographicCamera(1.0f,h/w); 

这意味着相机宽度为1px,高度为h / w ......例如,你的精灵就像是100x200。所以你唯一能看到的是精灵的1px。

请将其构造函数更改为以下内容:

float w = Gdx.graphics.getWidth(); //the width of the window
float h = Gdx.graphics.getHeight();//the height of the window

camera = new OrthographicCamera(w, h);

别忘了调整大小调整内部的相机。

可以在这里查看新的libgdx wiki:

  1. Orthographic camera
  2. Spritebatch, textureregions, and sprite