在 render() 方法上渲染新对象类型?

New object type render on render() method?

每次触摸屏幕或单击鼠标时,或者每次用户输入时,我都在寻找创建新对象的方法。我有一些想法,比如克隆、创建新的或添加 ArrayList。类似于:

if (Gdx.input.JustTouched){ shapeRenderer.circle(Gdx.input.getX(),Gdx.input.getY(),10); }

但每次触摸时,都会绘制一个新的圆圈。我试过:

private Object[] appendValue(Object[] obj, Object newObj) {

    ArrayList<Object> temp = new ArrayList<Object>(Arrays.asList(obj));
    temp.add(newObj);
    return temp.toArray();

但是我无法将 void 方法 (shapeRenderer.circle) 分配给对象,因此对我不起作用。另一种方法是在 if 之外进行绘制,传递坐标,但保留在渲染循环中,然后丢失坐标。是否有可能克隆方法并获得新的抽奖?如果有人回答,请告诉我,我会删除这个问题,我真的迷路了

方法 shapeRenderer.circle(float x, float y, float radius) 在 canvas 上画了一个圆。它不会创建对象,它会在 canvas 上创建一堆像素,一旦重绘 canvas,这些像素就会丢失。

您需要存储要传递给 circle 方法的数据,以便您可以重绘旧圆圈,并添加更多触摸事件。一个好的方法是定义一个 class Circle:

public class Circle {
    public float x;
    public float y;
    public float radius;

    public Circle(float x, float y, float radius) {
        this.x = x;
        this.y = y;
        this.radius = radius;
    }
}

然后您可以创建这些圆圈的列表,并在每次检测到触摸事件时添加一个新圆圈:

if (Gdx.input.JustTouched)
    circles.add(new Circle(Gdx.input.getX(),Gdx.input.getY(),10));

并且每当您需要重新绘制屏幕时,绘制所有 个圆圈:

for (Circle circle : circles)
    shapeRenderer.circle(circle.x, circle.y, circle.radius);

编辑

您的代码崩溃了,因为您的 Circles 集合为空,而您调用了 circles.add。除非您正确实例化 circles,否则这将引发空指针异常。

public class MyGdxGame implements ApplicationListener
{
    ShapeRenderer shapeRenderer;
    OrthographicCamera camera;

    // It's a collection specifically of Circles. Also, there's one per game, so don't make it static.
    Collection<Circle> circles;

    @Override
    public void create()
    {       
        camera = new OrthographicCamera();
        configureCamera();
        shapeRenderer = new ShapeRenderer();

        // Make it an empty collection to begin with
        circles = new ArrayList<Circle>();
    }

    @Override
    public void render()
    {       
        Gdx.gl.glClearColor(1, 1, 1, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        camera.update();
        shapeRenderer.setProjectionMatrix(camera.combined);
        shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);

        shapeRenderer.setColor(0, 0.5f, 0, 1);
        shapeRenderer.circle(50, 50, 40);

        shapeRenderer.setColor(0.5f, 0, 0, 1);
        shapeRenderer.rect(10, 100, 80, 80);

        shapeRenderer.setColor(0, 0, 0.5f, 1);
        shapeRenderer.triangle(10, 200, 90, 200, 50, 270);

        // Check for input, and add the new circle, *before* drawing all the circles
        if (Gdx.input.justTouched())
            circles.add(new Circle(Gdx.input.getX(),Gdx.input.getY(),10));

        for (Circle circle : circles)
            shapeRenderer.circle(circle.x, circle.y, circle.radius);

        shapeRenderer.end();
    }

    // *static* - Google the difference between inner vs nested classes
    public static class Circle {
        public float x;
        public float y;
        public float radius;

        public Circle(float x, float y, float radius) {
            this.x = x;
            this.y = y;
            this.radius = radius;
        }
    }
}

我想我会在这里做:

public class MyGdxGame implements ApplicationListener
{
ShapeRenderer shapeRenderer;
OrthographicCamera camera;
static Collection circles;

@Override
public void create()
{       
camera = new OrthographicCamera();
configureCamera();
circles = null;

shapeRenderer = new ShapeRenderer();
}

@Override
public void render()
{       
    Gdx.gl.glClearColor(1, 1, 1, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

camera.update();
shapeRenderer.setProjectionMatrix(camera.combined);
shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);

shapeRenderer.setColor(0, 0.5f, 0, 1);
shapeRenderer.circle(50, 50, 40);

shapeRenderer.setColor(0.5f, 0, 0, 1);
shapeRenderer.rect(10, 100, 80, 80);

shapeRenderer.setColor(0, 0, 0.5f, 1);
shapeRenderer.triangle(10, 200, 90, 200, 50, 270);


for (Circle circle : circles)
shapeRenderer.circle(circle.x, circle.y, circle.radius);

if (Gdx.input.justTouched())
    circles.add(new Circle(Gdx.input.getX(),Gdx.input.getY(),10));

shapeRenderer.end();
}


public class Circle {
   public float x;
   public float y;
   public float radius;

public Circle(float x, float y, float radius) {
    this.x = x;
    this.y = y;
    this.radius = radius;
}
}
}