我正在寻找每次触摸屏幕或点击鼠标或每次用户输入时创建新对象。我有一些想法就像克隆,创建新的或添加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之外进行绘制,传递坐标,但保持在渲染循环中然后丢失坐标。是否有可能克隆方法并获得新的抽奖?如果有人回答,请告诉我,我会删除这个问题,我真的失去了
答案 0 :(得分:0)
方法shapeRenderer.circle(float x, float y, float radius)
在画布上绘制一个圆圈。它不会创建一个对象,它会在画布上创建一堆像素,一旦重绘画布就会丢失这些像素。
您需要存储要传递到circle
方法的数据,以便您可以重绘旧圈子,并在触摸事件上添加更多内容。这样做的好方法是定义一个类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.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;
}
}
}
答案 1 :(得分:0)
想想我会在这里做:
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;
}
}
}