LibGDX。 试用基本形状。 希望舞台上有演员来获得触摸输入。 在分阶段添加到代码之前,它是否可以正常工作。 我可以让演员画在屏幕上,但是之后,我不确定该怎么做。 addListener似乎不再起作用。
Box.java
public class Box extends Actor {
private MyGdxGame game;
ShapeRenderer renderer;
private Vector3 pos;
private int width, height;
private Rectangle rect;
private Vector3 color;
public Box(MyGdxGame game) {
super();
this.game = game;
renderer = new ShapeRenderer();
// arbitrary x/y values
pos = new Vector3(300, 300, 0);
width = 300;
height = 200;
rect = new Rectangle(pos.x, pos.y, width, height);
color = new Vector3();
changeColor();
addListener(new ClickListener() {
@Override
public boolean touchDown(InputEvent event, float x, float y, int button, int pointer) {
changeColor();
return false;
}
});
}
private void changeColor() {
Random rng = new Random();
color.x = rng.nextInt(255) / 255f;
color.y = rng.nextInt(255) / 255f;
color.z = rng.nextInt(255) / 255f;
}
@Override
public void draw(Batch batch, float parentAlpha) {
super.draw(batch, parentAlpha);
renderer.begin(ShapeRenderer.ShapeType.Filled);
renderer.setColor(color.x, color.y, color.z, 1);
renderer.rect(pos.x, pos.y, width, height);
renderer.end();
}
public void dispose() {
renderer.dispose();
}
}
MyGDXGame.java
public class MyGdxGame implements ApplicationListener {
public OrthographicCamera camera;
private Stage stage;
@Override
public void create() {
camera = new OrthographicCamera();
camera.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
Box box = new Box(this);
stage = new Stage();
stage.addActor(box);
Gdx.input.setInputProcessor(stage);
}
@Override
public void render() {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
stage.act();
stage.draw();
}
@Override
public void dispose() {
renderer.dispose();
}
@Override
public void resize(int width, int height) {}
@Override
public void pause() {}
@Override
public void resume() {}
}
Edit2:
我删除了Box自己的位置和大小变量,但现在却抛出了各种错误。主要是IllegalStateExceptions和IllegalAccessErrors ...
致命异常:GLThread 16934 04-30 05:46:43.623 19181 19270 E AndroidRuntime进程:com.suchuu.box2dsquares,PID:19181 04-30 05:46:43.623 19181 19270 E AndroidRuntime java.lang.IllegalStateException: 04-30 05:46:43.623 19181 19270 E AndroidRuntime位于com.badlogic.gdx.graphics.glutils.ShapeRenderer.begin(ShapeRenderer.java:190) 04-30 05:46:43.623 19181 19270 E AndroidRuntime位于com.suchuu.box2dsquares.MyGdxGame.render(MyGdxGame.java:25) 04-30 05:46:43.623 19181 19270 E位于com.badlogic.gdx.backends.android.AndroidGraphics.onDrawFrame(AndroidGraphics.java:414)的AndroidRuntime 04-30 05:46:43.623 19181 19270 E AndroidRuntime at android.opengl.GLSurfaceView $ GLThread.guardedRun(GLSurfaceView.java:1571) 04-30 05:46:43.623 19181 19270 E AndroidRuntime at android.opengl.GLSurfaceView $ GLThread.run(GLSurfaceView.java:1270)
Box.java
public class Box extends Actor {
private MyGdxGame game;
ShapeRenderer renderer;
private Vector3 color;
public Box(MyGdxGame game) {
super();
this.game = game;
renderer = new ShapeRenderer();
setPosition(300, 300);
setBounds(300, 300, 600, 500);
color = new Vector3();
changeColor();
this.setTouchable(Touchable.enabled);
this.addListener(new ClickListener() {
@Override
public boolean touchDown(InputEvent event, float x, float y, int button, int pointer) {
changeColor();
event.handle();
return false;
}
});
}
private void changeColor() {
Random rng = new Random();
color.x = rng.nextInt(255) / 255f;
color.y = rng.nextInt(255) / 255f;
color.z = rng.nextInt(255) / 255f;
}
@Override
public void draw(Batch batch, float parentAlpha) {
super.draw(batch, parentAlpha);
renderer.begin();
renderer.set(ShapeRenderer.ShapeType.Filled);
renderer.setColor(color.x, color.y, color.z, 1);
renderer.rect(getX(), getY(), getWidth(), getHeight());
renderer.end();
}
public void dispose() {
renderer.dispose();
}
}
我真是个菜鸟。
答案 0 :(得分:1)
更改
renderer.begin();
renderer.set(ShapeRenderer.ShapeType.Filled);
收件人
renderer.begin(ShapeRenderer.ShapeType.Filled);