libGDX更新并呈现任务隔离

时间:2015-03-13 11:05:34

标签: java android libgdx

我正在尝试学习libGDX游戏开发的一些简单原则,并对下面的代码感到厌烦。我已经分离了更新和渲染的任务,但不太确定如何将它们组合在一起。我所要做的就是绘制一个在屏幕上移动的简单矩形。我的代码如下。除渲染外,一切正常。

package com.mygdx.gameobjects;

import com.badlogic.gdx.math.Rectangle;

public class Egg {

private Rectangle egg;

public Egg(){
    egg = new Rectangle(0, 0, 20, 20);
}

public void update(float delta) {
    egg.x++;
    if (egg.x > 137) {
        egg.x = 0;
    }
}

public Rectangle getEgg() {
    return egg;
}

}

使用的更新方法:

package com.mygdx.gameworld;

import com.badlogic.gdx.Gdx;
import com.mygdx.gameobjects.Egg;

public class GameWorld {

private Egg egg;

public GameWorld() {

    egg = new Egg();
}

public void update(float delta) {

    Gdx.app.log("GameWorld", "update");
    egg.update(delta);
}

public Egg getEgg() {
    return egg;

}
}

使用的渲染方法:

package com.mygdx.gameworld;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.mygdx.gameobjects.Egg;

public class GameRenderer {

private GameWorld myWorld;
private OrthographicCamera cam;
private ShapeRenderer shapeRenderer;
private Egg rect;


public GameRenderer(GameWorld world) {
    myWorld = world;
    cam = new OrthographicCamera();
    cam.setToOrtho(true, 136, 204);
    shapeRenderer = new ShapeRenderer();
    shapeRenderer.setProjectionMatrix(cam.combined);

}

public void render() {
    Gdx.app.log("GameRenderer", "render");
    Gdx.gl.glClearColor(0, 1, 1, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    shapeRenderer.begin(ShapeRenderer.ShapeType.Line);

    shapeRenderer.setColor(255 / 255.0f, 109 / 255.0f, 120 / 255.0f, 1);

    shapeRenderer.rect(myWorld.getEgg().x, myWorld.getEgg().y,
            myWorld.getEgg().width, myWorld.getEgg().height);

    shapeRenderer.end();


}
}

1 个答案:

答案 0 :(得分:0)

您需要将相机应用于形状渲染器。

shapeRenderer.setProjectionMatrix(cam.combined);放在shapeRenderer.begin之前。