目前,iam编程在一个游戏中,你移动一艘宇宙飞船,并试图避免小行星。当用户接触到飞船时,飞船应该移动,因此要跟随用户的手指移动。宇宙飞船是一种精灵,它围绕着:
if (Gdx.input.isTouched()) {
x = Gdx.input.getX() - width / 2;
y = -Gdx.input.getY() + height / 2;
}
我现在遇到的问题是,用户可以通过触摸屏幕传送宇宙飞船。我怎么才能解决这个问题?有可能设置一个触摸区域吗?
发布于 2016-08-04 14:12:07
计算出从船到接触点的单位矢量方向,并将其乘以速度。您需要将触摸坐标转换为世界坐标,通过取消投影与相机。
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;
}
}
https://stackoverflow.com/questions/38768543
复制相似问题