I am trying to get a simple rectangle to move across the screen. I have defined my object and segregated the task of update and render. I know I have to put something in Gamerenderer, but don't know what code to use to reference my object. Any help would be much appreciated. Thanks
My object:
package com.mygdx.gameobjects;
import com.badlogic.gdx.math.Rectangle;
public class Egg {
private Rectangle rect;
public Egg(){
rect = new Rectangle(0, 0, 50, 50);
}
public void update(float delta) {
rect.x++;
if (rect.x > 137) {
rect.x = 0;
}
}
public Rectangle getRect() {
return rect;
}
}
My Gameworld:
package com.mygdx.gameworld;
import com.badlogic.gdx.Gdx;
import com.mygdx.gameobjects.Egg;
public class GameWorld {
Egg rect = new Egg();
public void update(float delta) {
Gdx.app.log("GameWorld", "update");
rect.update(delta);
}
public Egg getRect() {
return rect;
}
}
GameRenderer
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;
public class GameRenderer {
private GameWorld myWorld;
private OrthographicCamera cam;
private ShapeRenderer shapeRenderer;
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(xxxxxxxxxxxx);
shapeRenderer.end();
}
}
What do I need to put between the brackets to see my rectangle i.e. xxxxxx should be replaced with what?
Put
Rectangle eggRect = myWorld.rect.getRect();
on the line above. This gets a reference to your rectangle. Then replace your xxxxx with the relevant values:
(eggRect.x, eggRect.y, eggRect.width, eggRect.height)
The method is overloaded, do you can also add arguments for colour and so on if you want to