我有这两个纹理
ballImage = new Texture(Gdx.files.internal("ball.png"));
spikeImage = new Texture(Gdx.files.internal("spike.png"));
单击时,我正在检查一个条件,根据该条件,我将绘制与矩形(保持在阵列中)相关的ballImage或spikeImage。如下图所示,
if (Gdx.input.isTouched()) {
Vector3 touchPos = new Vector3();
touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
camera.unproject(touchPos);
if (isSpike()) {
spawnSpike(touchPos);
} else {
spawnBalls(touchPos);
}
}
这是一种产卵方法(另一种类似),
private void spawnSpike(Vector3 touchPos) {
Spike spike = new Spike();
spike.setRectX((int) touchPos.x);
spike.setRectY((int) touchPos.y);
spike.setRectWidth(64);
spike.setRectHeight(48);
spikes.add(spike);
}
渲染中的图形是,
for (Ball ball : balls) {
game.batch.draw(ballImage, ball.getRectX(), ball.getRectY());
}
for (Spike spike : spikes) {
game.batch.draw(spikeImage, spike.getRectX(), spike.getRectY());
}
问题:
在触摸事件中,当isSpike()为假时,一切都很好,它绘制了正确的纹理(球),但当它为真时,它应该绘制spikeImage,但由于某种原因,在之前绘制了ballImage,spikeImage绘制在其顶部(重叠)。为什么会这样,我做错了什么。
我认为您只需要删除camera.unproject(touchpos)
此外,在获取输入时使用事件,如下所示:
1) 在班级乞讨上加上implements InputProcessor
。
2) 添加到您的代码:
public boolean keyDown (int keycode);
public boolean keyUp (int keycode);
public boolean keyTyped (char character);
public boolean touchDown (int screenX, int screenY, int pointer, int button);
public boolean touchUp (int screenX, int screenY, int pointer, int button);
public boolean touchDragged (int screenX, int screenY, int pointer);
public boolean mouseMoved (int screenX, int screenY);
public boolean scrolled (int amount);
在函数之前使用CCD_ 3。
3)如果使用函数或return false;
,则添加return true;
。
4)添加Gdx.input.setInputProcessor(this);
示例:
@Override
public boolean keyDown(int keycode){
if(keycode == Input.Keys.A){
player.moveBy(-10, 0);
}
return true;
}
祝你好运!