当摄像机坐标与窗口坐标不匹配时,Java libGDX不会绘制精灵

时间:2015-06-17 13:44:21

标签: java libgdx

我遇到了libGDX引擎的问题。我有一对变量用于设置屏幕的宽度和高度,另一对用于设置摄像机的宽度和高度。但是,每当相机与屏幕尺寸不同时,引擎就不会渲染任何精灵。以下是我的代码供您查看:

我的常数:

public abstract class Constants 
{
    public static final int WINDOW_WIDTH = 800;
    public static final int WINDOW_HEIGHT = 600;

    public static final int CAMERA_WIDTH = 800;
    public static final int CAMERA_HEIGHT = 600;
}

创建精灵的位置:

public class GameController 
{
    public static final String TAG = GameController.class.getName();

    public Sprite[] testSprites;
    public int selectedSprite;

    public GameController()
    {
        init();
    }

    private void init()
    {
        initTestObjects();
    }

    private void initTestObjects()
    {
        testSprites = new Sprite[1];

        int width = 32;
        int height = 32;

        Pixmap pixmap = createProceduralPixmap(width, height);

        Texture texture = new Texture(pixmap);

        for(int i = 0; i < testSprites.length; i++)
        {
            Sprite spr = new Sprite(texture);

            spr.setSize(32, 32);
            spr.setOrigin(spr.getWidth() / 2.0f, spr.getHeight() / 2.0f);

            spr.setPosition(0, 0);

            testSprites[i] = spr;
        }

        selectedSprite = 0;
    }

    private Pixmap createProceduralPixmap(int width, int height)
    {
        Pixmap pixmap = new Pixmap(width, height, Format.RGBA8888);
        pixmap.setColor(1, 0, 0, 0.5f);
        pixmap.fill();

        pixmap.setColor(1, 1, 0, 1);
        pixmap.drawLine(0,  0, width, height);
        pixmap.drawLine(width, 0, 0, height);

        pixmap.setColor(0, 1, 1, 1);
        pixmap.drawRectangle(0, 0, width, height);

        return pixmap;
    }

    public void update(float deltaTime)
    {
        updateTestObjects(deltaTime);
    }

    public void updateTestObjects(float deltaTime)
    {
        float rotation = testSprites[selectedSprite].getRotation();
        rotation += 90 * deltaTime;
        rotation %= 360;
        testSprites[selectedSprite].setRotation(rotation);
    }
}

绘图:

public class GameRenderer implements Disposable
{
    public static final String TAG = GameRenderer.class.getName();

    private OrthographicCamera camera;
    private SpriteBatch batch;
    private GameController gameController;

    public GameRenderer(GameController gameController)
    {
        this.gameController = gameController;
        init();
    }

    private void init()
    {
        batch = new SpriteBatch();
        camera = new OrthographicCamera(Constants.CAMERA_WIDTH, 
                                        Constants.CAMERA_HEIGHT);
        camera.position.set(0, 0, 0);
        camera.update();
    }

    public void render()
    {
        renderTestObjects();
    }

    private void renderTestObjects()
    {
        batch.setProjectionMatrix(camera.combined);
        batch.begin();

        for(Sprite sprite : gameController.testSprites)
        {
            sprite.draw(batch);
        }

        batch.end();
    }

    public void resize(int width, int height)
    {
        camera.viewportWidth = 
            (Constants.CAMERA_HEIGHT / height) * width;

        camera.update();
    }

    @Override
    public void dispose()
    {
        batch.dispose();
    }

}

上面的代码工作正常并且会在屏幕的中心呈现一个精灵,但是当我将CAMERA_WIDTH和CAMERA_HEIGHT分别改为80和60时,而不是像它应该那样使精灵变大10倍,它不会绘制任何东西。有什么建议吗?

1 个答案:

答案 0 :(得分:3)

你用整数划分整数,所以你得到的数字很差。例如:

camera.viewportWidth = 
        (Constants.CAMERA_HEIGHT / height) * width;

如果屏幕宽度和高度分别为800和600,CAMERA_HEIGHT为60,那么等式变为

camera.viewportWidth = 
        (60 / 600) * 800;

变为(由于纯整数数学):

camera.viewportWidth = 
        (0) * 800;

变为0.你需要在划分之前将这些整数投射到浮点数!这可能不是唯一的问题,但仅此一项可能会导致您的问题。