使用libgdx触摸滚动
我正试图在libgdx游戏中实现触摸滚动。我有一个宽阔的形象,是一个房间的全景。我希望能够滚动图像,以便用户可以看到房间周围。我有它,这样我可以滚动一定的距离,但是当一个新的touchDragged事件被注册时,图像会移回到原始位置。使用libgdx触摸滚动
这是我如何实现它
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 corodinates不是世界坐标,但我试图unprojecting相机没有任何效果。
我一直在为此奋斗了几个星期,我真的很感谢这方面的帮助。
在此先感谢。
我最近做了一件你想要的东西。这是我使用的移动地图我的Input类,你只需要改变我的'舞台。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
我没有用它自己,但我会通过查看的代码开始:
你看着http://libgdx.l33tlabs.org/docs/api/com/badlogic/gdx/scenes/scene2d/ui/FlickScrollPane.html
看到代码:touchDragged
谢谢,这看起来很有趣我看着ScrollPane中,而是被抛弃。它会有滚动条,这不是我想要的,但它看起来像没有它们,因此可能是完美的。 – jonnypotwash 2013-02-27 15:11:24
在最糟糕的情况下,您可以找到一些代码,它接近您想要实现的目标... – 2013-02-27 15:15:04
Just从6月底发现一个Badlogic Games博客,指出FlickScollPane不再使用,但ScrollPane拥有所有的功能,滚动条可绘制是可选的,所以它看起来像Scrollpane是现在出发。 – jonnypotwash 2013-02-27 15:26:26
你需要做在touchDragged
回调中有很多计算,您不能将任何屏幕坐标都传递给相机。你需要弄清楚用户如何将远远的拖到他们的手指上,并朝着什么方向。绝对坐标不是立即有用的。
考虑从右上方拖拽或从左上方拖拽。在这两种情况下,您都希望(我认为)将相机移动的距离相同,但屏幕坐标的绝对值在两种情况下会有很大差异。
我认为最简单的做法是只跟踪previousX
和previousY
(在touchDown
方法对它们进行初始化,然后用三角(deltaX = screenX - previousX
调用cam.translate()
,例如),中touchDragged
,而且还更新touchDragged
的previous*
。
或者,你可以看一些发烧友InputProcessor
包装libgdx提供(见https://code.google.com/p/libgdx/wiki/InputGestureDetection)的。
答案很简单:
申报2场举行新老拖动位置:
Vector2 dragOld, dragNew;
刚刚碰触时,将这两个设置为等于触摸的位置,否则您的凸轮将跳起。
if (Gdx.input.justTouched())
{
dragNew = new Vector2(Gdx.input.getX(), Gdx.input.getY());
dragOld = dragNew;
}
更新dragNew每帧,并简单地相互减去向量得到X和Y翻译相机。
if (Gdx.input.isTouched())
{
dragNew = new Vector2(Gdx.input.getX(), Gdx.input.getY());
if (!dragNew.equals(dragOld))
{
cam.translate(dragOld.x - dragNew.x, dragNew.y - dragOld.y); //Translate by subtracting the vectors
cam.update();
dragOld = dragNew; //Drag old becomes drag new.
}
}
这是我用来拖动我的ortho凸轮四周,简单而有效。
Gdx.input.justTouched()在我的设备1中不工作..任何替代方法 – iappmaker 2015-06-19 04:55:54
使用drinor的回答倒是对“触地线)功能,所以它不会在每次启动再拖动时间重置相机:
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
last_touch_down.set(screenX, screenY, 0);
return false;
}
这看起来可能是答案,你能否请你包含你的cameraOutOfLimit实现,因为这是我遇到的问题。我已经尝试了一个Scrollpane,并且这个方法可行,但是我可以在场景中添加一个新的Actor并让它随着背景一起滚动。 – jonnypotwash 2013-03-08 17:25:42
我添加了cameraOutOfLimit函数。我希望这会有所帮助。 – drinor 2013-03-08 23:15:25
这与P.T.提出的gestureListener结合使用效果很好。下面。 – jonnypotwash 2013-03-12 15:18:47