libGDX shaperenderer代码链接移动对象

时间:2015-03-14 10:09:22

标签: java android libgdx

我想在屏幕上移动一个简单的矩形。我已经定义了我的对象并隔离了更新和渲染的任务。我知道我必须在Gamerenderer中放一些东西,但不知道用什么代码来引用我的对象。任何帮助将非常感激。感谢

我的对象:

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;
    }
}

我的游戏世界:

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();
    }
}

我需要在括号之间放置什么来查看我的矩形,即xxxxxx应该替换为什么?

1 个答案:

答案 0 :(得分:1)

Rectangle eggRect = myWorld.rect.getRect();

在上面的行。这将获得对矩形的引用。然后将xxxxx替换为相关值:

(eggRect.x, eggRect.y, eggRect.width, eggRect.height)

方法重载,如果你想

,你还可以为颜色添加参数吗?