LibGDX 舞台上的演员没有收到点击事件
LibGDX Actor in stage not receiving click events
编辑:
我认为这与我 setBounds() 的方式有关...
我有一个类似的项目(实际上有效),涉及一个移动的盒子,点击时会改变颜色......一个盒子 - 而不是一个圆圈。我认为这是一个线索。
OGPost:
- 子类 Actor。
- 将演员放在舞台上。
- 在 Actor.constructor 中添加了一个 ClickListener。
- 将舞台设置为 inputProcessor。
- 但是当我点击 Actor 时,似乎没有任何反应。
没有反应 - 当我尝试点击演员时。
假设改变颜色,提高速度和跟踪输入x/ys。
这很奇怪,因为我构建了一个非常相似的有效程序。那为什么不呢?
演员由 ShapeRenderer 绘制的圆圈表示。我假设它的 "click" 边界必须只是一个矩形?
HelloGDXGame.java
@Override
public void create() {
SCREEN_WIDTH = Gdx.graphics.getWidth() / 2;
SCREEN_HEIGHT = Gdx.graphics.getHeight() / 2;
rowHeight = 80;
touchPos = new Vector3();
batch = new SpriteBatch();
font = new BitmapFont();
font.setColor(Color.PINK);
font.setScale(4.0f);
ball = new Ball(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, 180, 90, this);
stage = new Stage(new StretchViewport(SCREEN_WIDTH, SCREEN_HEIGHT));
stage.addActor(ball);
Gdx.input.setInputProcessor(stage);
}
@Override
public void render()
{
Gdx.gl.glClearColor(0.5f, 0.5f, 0.5f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
// Translate inputs x/y
touchPos.x = Gdx.input.getX();
touchPos.y = (Gdx.input.getY() - Gdx.graphics.getHeight()) * -1;
stage.act();
stage.draw();
// UI, to check it all works (it doesn't)
batch.begin();
font.draw(batch, "Ball Class: Taps: " + ball.getTaps(), 64, SCREEN_HEIGHT - (rowHeight * 8));
font.draw(batch, "Main Class: Touch X: " + touchPos.x, 64, SCREEN_HEIGHT - (rowHeight * 7));
font.draw(batch, "Main Class: Touch Y: " + touchPos.y, 64, SCREEN_HEIGHT - (rowHeight * 6));
font.draw(batch, "Ball Class: Touch X: " + ball.getScreenX(), 64, SCREEN_HEIGHT - (rowHeight * 5));
font.draw(batch, "Ball Class: Touch Y: " + ball.getScreenY(), 64, SCREEN_HEIGHT - (rowHeight * 4));
batch.end();
}
Ball.java
public Ball(float xPos, float yPos, float xSpeed, float ySpeed, HelloGdxGame game) {
super();
this.game = game;
renderer = new ShapeRenderer();
taps = 0;
radius = 196;
super.setBounds(xPos - radius, yPos - radius, radius * 2, radius * 2);
// just to check it's working
screenXY = new Vector3(0, 0, 0);
this.xSpeed = xSpeed;
this.ySpeed = ySpeed;
this.setTouchable(Touchable.enabled);
this.addListener(new ClickListener() {
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
doStuff(x, y);
return false;
}
});
changeColour(); // sets random color
}
private void doStuff(float screenX, float screenY) {
screenXY.x = screenX;
screenXY.y = screenY;
changeColour();
taps++;
// Increase speed
if (xSpeed > 0) xSpeed++;
else xSpeed--;
if (ySpeed > 0) ySpeed++;
else ySpeed--;
}
@Override
public void act(float delta) {
super.act(delta);
// move
super.setX(super.getX() + (xSpeed * delta));
super.setY(super.getY() + (ySpeed * delta));
// Bounce horizontally
if (super.getX() < 0 || super.getX() + (radius / 2) > Gdx.graphics.getWidth()) {
super.setX(super.getX() - (xSpeed * delta));
xSpeed *= -1;
}
// Bounce vertically
if (super.getY() < 0 || super.getY() + (radius / 2) > Gdx.graphics.getHeight()) {
super.setY(super.getY() - (ySpeed * delta));
ySpeed *= -1;
}
}
@Override
public void draw(Batch batch, float parentAlpha) {
super.draw(batch, parentAlpha);
renderer.begin(ShapeRenderer.ShapeType.Filled);
renderer.setColor(r, g, b, 1);
renderer.circle(super.getX(), super.getY(), radius);
renderer.end();
}
1) 从here
拿一个改变的球
2)
更改
SCREEN_WIDTH = Gdx.graphics.getWidth() / 2;
SCREEN_HEIGHT = Gdx.graphics.getHeight() / 2;
到
SCREEN_WIDTH = Gdx.graphics.getWidth();
SCREEN_HEIGHT = Gdx.graphics.getHeight();
这不合逻辑。
编辑:
我认为这与我 setBounds() 的方式有关... 我有一个类似的项目(实际上有效),涉及一个移动的盒子,点击时会改变颜色......一个盒子 - 而不是一个圆圈。我认为这是一个线索。
OGPost:
- 子类 Actor。
- 将演员放在舞台上。
- 在 Actor.constructor 中添加了一个 ClickListener。
- 将舞台设置为 inputProcessor。
- 但是当我点击 Actor 时,似乎没有任何反应。
没有反应 - 当我尝试点击演员时。 假设改变颜色,提高速度和跟踪输入x/ys。
这很奇怪,因为我构建了一个非常相似的有效程序。那为什么不呢?
演员由 ShapeRenderer 绘制的圆圈表示。我假设它的 "click" 边界必须只是一个矩形?
HelloGDXGame.java
@Override
public void create() {
SCREEN_WIDTH = Gdx.graphics.getWidth() / 2;
SCREEN_HEIGHT = Gdx.graphics.getHeight() / 2;
rowHeight = 80;
touchPos = new Vector3();
batch = new SpriteBatch();
font = new BitmapFont();
font.setColor(Color.PINK);
font.setScale(4.0f);
ball = new Ball(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, 180, 90, this);
stage = new Stage(new StretchViewport(SCREEN_WIDTH, SCREEN_HEIGHT));
stage.addActor(ball);
Gdx.input.setInputProcessor(stage);
}
@Override
public void render()
{
Gdx.gl.glClearColor(0.5f, 0.5f, 0.5f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
// Translate inputs x/y
touchPos.x = Gdx.input.getX();
touchPos.y = (Gdx.input.getY() - Gdx.graphics.getHeight()) * -1;
stage.act();
stage.draw();
// UI, to check it all works (it doesn't)
batch.begin();
font.draw(batch, "Ball Class: Taps: " + ball.getTaps(), 64, SCREEN_HEIGHT - (rowHeight * 8));
font.draw(batch, "Main Class: Touch X: " + touchPos.x, 64, SCREEN_HEIGHT - (rowHeight * 7));
font.draw(batch, "Main Class: Touch Y: " + touchPos.y, 64, SCREEN_HEIGHT - (rowHeight * 6));
font.draw(batch, "Ball Class: Touch X: " + ball.getScreenX(), 64, SCREEN_HEIGHT - (rowHeight * 5));
font.draw(batch, "Ball Class: Touch Y: " + ball.getScreenY(), 64, SCREEN_HEIGHT - (rowHeight * 4));
batch.end();
}
Ball.java
public Ball(float xPos, float yPos, float xSpeed, float ySpeed, HelloGdxGame game) {
super();
this.game = game;
renderer = new ShapeRenderer();
taps = 0;
radius = 196;
super.setBounds(xPos - radius, yPos - radius, radius * 2, radius * 2);
// just to check it's working
screenXY = new Vector3(0, 0, 0);
this.xSpeed = xSpeed;
this.ySpeed = ySpeed;
this.setTouchable(Touchable.enabled);
this.addListener(new ClickListener() {
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
doStuff(x, y);
return false;
}
});
changeColour(); // sets random color
}
private void doStuff(float screenX, float screenY) {
screenXY.x = screenX;
screenXY.y = screenY;
changeColour();
taps++;
// Increase speed
if (xSpeed > 0) xSpeed++;
else xSpeed--;
if (ySpeed > 0) ySpeed++;
else ySpeed--;
}
@Override
public void act(float delta) {
super.act(delta);
// move
super.setX(super.getX() + (xSpeed * delta));
super.setY(super.getY() + (ySpeed * delta));
// Bounce horizontally
if (super.getX() < 0 || super.getX() + (radius / 2) > Gdx.graphics.getWidth()) {
super.setX(super.getX() - (xSpeed * delta));
xSpeed *= -1;
}
// Bounce vertically
if (super.getY() < 0 || super.getY() + (radius / 2) > Gdx.graphics.getHeight()) {
super.setY(super.getY() - (ySpeed * delta));
ySpeed *= -1;
}
}
@Override
public void draw(Batch batch, float parentAlpha) {
super.draw(batch, parentAlpha);
renderer.begin(ShapeRenderer.ShapeType.Filled);
renderer.setColor(r, g, b, 1);
renderer.circle(super.getX(), super.getY(), radius);
renderer.end();
}
1) 从here
拿一个改变的球2) 更改
SCREEN_WIDTH = Gdx.graphics.getWidth() / 2;
SCREEN_HEIGHT = Gdx.graphics.getHeight() / 2;
到
SCREEN_WIDTH = Gdx.graphics.getWidth();
SCREEN_HEIGHT = Gdx.graphics.getHeight();
这不合逻辑。