我正在尝试在libgdx游戏中实现触摸滚动。我有一张宽阔的图片,是一个房间的全景图。我希望能够滚动图片,让用户能够看到房间周围的景象。我已经实现了滚动一定距离的功能,但当新的touchDragged事件被注册时,图片会被移回原始位置。
这是我的实现方式:
public class AttackGame implements ApplicationListener {
AttackInputProcessor inputProcessor;
Texture backgroundTexture;
TextureRegion region;
OrthographicCamera cam;
SpriteBatch batch;
float width;
float height;
float posX;
float posY;
@Override
public void create() {
posX = 0;
posY = 0;
width = Gdx.graphics.getWidth();
height = Gdx.graphics.getHeight();
backgroundTexture = new Texture("data/pancellar.jpg");
region = new TextureRegion(backgroundTexture, 0, 0, width, height);
batch = new SpriteBatch();
}
@Override
public void resize(int width, int height) {
cam = new OrthographicCamera();
cam.setToOrtho(false, width, height);
cam.translate(width / 2, height / 2, 0);
inputProcessor = new AttackInputProcessor(width, height, cam);
Gdx.input.setInputProcessor(inputProcessor);
}
@Override
public void render() {
Gdx.gl.glClearColor(0,0,0,1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
batch.setProjectionMatrix(cam.combined);
batch.begin();
batch.draw(backgroundTexture, 0, 0, 2400, 460);
batch.end();
}
@Override
public void pause() {
// TODO Auto-generated method stub
}
@Override
public void resume() {
// TODO Auto-generated method stub
}
@Override
public void dispose() {
backgroundTexture.dispose();
}
在InputProcessor中
@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
cam.position.set(screenX, posY / 2, 0);
cam.update();
return false;
}
我在这个问题的帮助下有所进展:LibGdx How to Scroll using OrthographicCamera?。然而,它并没有真正解决我的问题。
我认为问题出在touchDragged坐标不是世界坐标,但我已经尝试使用unprojecting相机来解决,但没有效果。
我已经苦苦挣扎了几周,真的很需要一些帮助。
提前感谢。