gpt4 book ai didi

java - 使用 libgdx 触摸滚动

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:57:40 24 4
gpt4 key购买 nike

我正在尝试在 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 坐标不是世界坐标,但我尝试取消投影相机但没有效果。

几周来我一直在为这个问题苦苦挣扎,非常感谢能提供一些帮助。

提前致谢。

最佳答案

我最近做了一些你想要的事情。这是我用于移动 map 的输入类,您只需为您的“cam”更改我的“stage.getCamera()”:

public class MapInputProcessor implements InputProcessor {
Vector3 last_touch_down = new Vector3();

...

public boolean touchDragged(int x, int y, int pointer) {
moveCamera( x, y );
return false;
}

private void moveCamera( int touch_x, int touch_y ) {
Vector3 new_position = getNewCameraPosition( touch_x, touch_y );

if( !cameraOutOfLimit( new_position ) )
stage.getCamera().translate( new_position.sub( stage.getCamera().position ) );

last_touch_down.set( touch_x, touch_y, 0);
}

private Vector3 getNewCameraPosition( int x, int y ) {
Vector3 new_position = last_touch_down;
new_position.sub(x, y, 0);
new_position.y = -new_position.y;
new_position.add( stage.getCamera().position );

return new_position;
}

private boolean cameraOutOfLimit( Vector3 position ) {
int x_left_limit = WINDOW_WIDHT / 2;
int x_right_limit = terrain.getWidth() - WINDOW_WIDTH / 2;
int y_bottom_limit = WINDOW_HEIGHT / 2;
int y_top_limit = terrain.getHeight() - WINDOW_HEIGHT / 2;

if( position.x < x_left_limit || position.x > x_right_limit )
return true;
else if( position.y < y_bottom_limit || position.y > y_top_limit )
return true;
else
return false;
}


...
}

这是结果:http://www.youtube.com/watch?feature=player_embedded&v=g1od3YLZpww

关于java - 使用 libgdx 触摸滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15115282/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com