创建一个继承InputAdapter类的类,然后创建它的一个实例并注册它来监听触摸输入。
Gdx.input.setInputProcessor(inputAdapter);
有 3 种方法可以处理您必须覆盖的触摸事件 touch_down、touch_dragged 和 touch_up。
在touch_down中,检查触摸位置是否在鸟区。如果是,则将布尔标志设为 true。
在touch_dragged中,检查上面的标志,如果是真的,计算触摸位置相对于小鸟射击中心的距离和射击角度。
在touch_up,您可以通过调用来订购计算数量的拍摄
body2shoot.applyLinearImpulse(impulse, body2shoot.getWorldCenter());
无需MouseJointDef 即可移动body2shoot。只需将body2shoot的变换设置在触摸位置,以便在每个渲染周期中拖动即可。
为了计算轨迹,我写了一个这样的类:
public class ProjectileEquation
{
public float gravity;
public Vector2 startVelocity = new Vector2();
public Vector2 startPoint = new Vector2();
public ProjectileEquation()
{ }
public float getX(float t)
{
return startVelocity.x*t + startPoint.x;
}
public float getY(float t)
{
return 0.5f*gravity*t*t + startVelocity.y*t + startPoint.y;
}
}
为了绘制它,我设置了startPoint 和startVelocity,然后在循环中我逐步给出t(时间)并调用getX(t) 和getY(t)。