创建一个继承的类InputAdapter
类,然后创建它的实例并注册它以监听触摸输入。
Gdx.input.setInputProcessor(inputAdapter);
有3种方法来处理触摸事件touch_down
, touch_dragged
and touch_up
你必须重写。
In touch_down
,检查触摸位置是否在鸟类区域内。如果是,则将布尔标志设置为 true。
In touch_dragged
,检查上面的flag,如果为真,计算触摸位置相对于小鸟射击中心的距离和射击角度。
In 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
and startVelocity
然后在循环中我给出一个t
(时间)增量并调用getX(t)
and getY(t)
.