触摸屏幕移动矩形 [英] Move rectangle by touching the screen

查看:114
本文介绍了触摸屏幕移动矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望移动具有不同x位置的块而不通过减小x位置来改变它们的形状。

I want to move blocks with different x-positions without changing their shape by reducing the x-position.

我试图运行以下代码,但它看起来像块移动到拖曳位置的方式来快速(正确的药水和其他我看不到的地方)。

I have tried to run the following code, but it seems like the blocks move to a tow position way to fast (correct potion and other i can't see where).

downBlocks=new Arraylist<Rectangle>;
for (DownBlocks downBlocks:getBlocks()){
    if(Gdx.input.isTouched()) {
        Vector3 touchPos = new Vector3();

        touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
        camera.unproject(touchPos);

        downBlocks.x = (int) touchPos.x - downBlocks.x;
    }
}


推荐答案

到拖动,您需要记住手指最后触摸屏幕的点,这样您就可以获得手指增量。作为旁注,如果只需要调用一次代码,请避免将代码放入循环迭代中。每个DownBlock都反复取消屏幕触摸点的投射是浪费的。

To do a drag, you need to remember the point where the finger last touched the screen so you can get a finger delta. And as a side note, avoid putting code inside your loop iteration if it only needs to be called once. It's wasteful to unproject the screen's touch point over and over for every one of your DownBlocks.

static final Vector3 VEC = new Vector3(); // reusuable static member to avoid GC churn
private float lastX; //member variable for tracking finger movement

//In your game logic:
if (Gdx.input.isTouching()){
    VEC.set(Gdx.input.getX(), Gdx.input.getY(), 0);
    camera.unproject(VEC);
}

if (Gdx.input.justTouched())
    lastX = VEC.x; //starting point of drag
else if (Gdx.input.isTouching()){ // dragging
    float deltaX = VEC.x - lastX; // how much finger has moved this frame
    lastX = VEC.x; // for next frame

    // Since you're working with integer units, you can round position
    int blockDelta = (int)Math.round(deltaX);

    for (DownBlocks downBlock : getBlocks()){
        downBlock.x += blockDelta;
    }
}

我不建议使用整数单位作为坐标但是。如果您正在进行像素艺术,那么我建议使用浮点数来存储坐标,并仅在绘制时舍入坐标。这将减少看起来不稳定的运动。如果你不使用像素艺术,我会一直使用浮点坐标。 这是一篇很好的文章,以帮助理解单位。

I don't recommend using integer units for your coordinates, though. If you are doing pixel art, then I recommend using floats for storing coordinates, and rounding off the coordinates only when drawing. That will reduce jerky-looking movement. If you are not using pixel art, I would just use float coordinates all the way. Here's a good article to help understand units.

这篇关于触摸屏幕移动矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆