0
我正在嘗試學習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();
}
}
謝謝你的幫助。沒有工作。還有其他建議嗎? – Learningmind 2015-03-14 10:12:01
您確定例如我的座標是否在相機的視野內?這是我唯一能想到的其他事情。 – Tenfour04 2015-03-14 15:39:17
座標絕對在相機的視野中。如果我直接在GameWorld類中編寫Egg類代碼,那麼一切正常。但這個想法是讓GameWorld只能更新。 – Learningmind 2015-03-14 17:17:57