移动形状到触摸手指的地方 [英] Move a shape to place where my finger is touched

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

问题描述

@Override
public void create()
{

    batch = new SpriteBatch();

    shape = new ShapeRenderer();
    velocity = new Vector2(100, 0);
    position = new Rectangle(0, 5, 100, 100);

    font = new BitmapFont();
    font.setColor(Color.BLACK);
    font.getData().scale(3f);

    camera = new OrthographicCamera();
    confCamera();

}

@Override
public void render()
{        

       if(Gdx.input.isTouched())
        position.x = Gdx.input.getX() - position.width/2;
        position.y = (Gdx.input.getY()  - position.height/2);

    shape.begin(ShapeRenderer.ShapeType.Filled);

    shape.setColor(Color.BLACK);
    shape.rect(position.x, position.y, position.width, position.height);

    shape.end();
}

这是一个简单的代码,但是我并不是不理解Y轴,我的形状像镜子一样移动.如果我触摸顶部,我的形状将移至底部.如果我触摸底部,则形状将移至顶部.如何解决?

It's a simple code, but I'm not undestanding Y axis, my shape moves like a mirror. If I touch on top, my shape goes to bottom. If I touch on bottom, my shape goes to top. How to fix it?

推荐答案

用于渲染的LibGDX(默认情况下)使用的坐标系是0,位于屏幕底部,并且越向上,Y坐标就越大.

LibGDX (by default) for rendering uses coordinate system where 0 is at bottom of the screen and the more you go up Y coordinate grows.

此外,当您读取输入坐标(触摸,移动...)时,您将获得屏幕坐标,但是在渲染图形时,您将使用世界"坐标.它们处于2个不同的坐标系中,因此要从屏幕转换为世界,您必须使用camera.unproject()调用.应该是这样的:

Also, when you read input coordinates (touches, moves...) you get screen coordinates, but when you render your graphics you are using "world" coordinates. They are in 2 different coordinate system so to convert from screen to world you have to use camera.unproject() call. Should be like:

Vector3 touchPos = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
camera.unproject(touchPos);

,然后使用touchPos.xtouchPos.y.

这里会问类似的问题,以便您可以在此处找到更多答案:

The similar question is asked here so you can find more answers there:

在Java Libgdx中正确使用unProject

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

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