【问题标题】:Android/Libgdx How to fix this issue?Android/Libgdx 如何解决这个问题?
【发布时间】:2016-08-04 13:20:55
【问题描述】:

目前我正在为一款游戏进行编程,您可以在其中移动宇宙飞船并尝试避开小行星。当用户触摸它时,宇宙飞船应该移动,因此跟随用户的手指移动。 宇宙飞船是一个四处移动的精灵:

if (Gdx.input.isTouched()) {  

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

} 

我现在遇到的问题是用户可以通过触摸屏幕来传送飞船。我怎样才能解决这个问题?是否可以设置触摸区域?

【问题讨论】:

  • 先检查用户触屏的时候,如果触屏的位置是飞船..如果不是什么都不做则跟随运动

标签: java android libgdx


【解决方案1】:

计算从船到接触点的单位矢量方向并将其乘以速度。您需要通过相机取消投影将触摸坐标转换为世界坐标。

private static final float SHIP_MAX_SPEED = 50f; //units per second
private final Vector2 tmpVec2 = new Vector2();
private final Vector3 tmpVec3 = new Vector3();

//...

if (Gdx.input.isTouched()) {  
    camera.unproject(tmpVec3.set(Gdx.input.getX(), Gdx.input.getY(), 0)); //touch point to world coordinate system.
    tmpVec2.set(tmpVec3.x, tmpVec3.y).sub(x, y); //vector from ship to touch point
    float maxDistance = SHIP_MAX_SPEED * Gdx.graphics.getDeltaTime(); //max distance ship can move this frame
    if (tmpVec2.len() <= maxDistance) {
        x = tmpVec3.x;
        y = tmpVec3.y;
    } else {
        tmpVec2.nor().scl(maxDistance); //reduce vector to max distance length
        x += tmpVec2.x;
        y += tmpVec2.y;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-21
    • 2018-04-15
    • 1970-01-01
    • 2014-10-07
    • 1970-01-01
    相关资源
    最近更新 更多