Java Android虚拟操纵杆

时间:2014-12-28 11:38:35

标签: java android joystick

我尝试为我的安卓游戏创建自己的自定义操纵杆,当我试图平滑操纵杆动画时,我遇到了一些问题。我制作了一个球体并为它创建了一个边界圆,然后将操纵杆放在球体内(制作了一个也是操纵杆的边界圆)。因此,每当操纵杆的边界圆与球体的蜿蜒圆相交时,操纵杆就会停止。我设法停止操纵杆但它仍然卡住。我希望它能够将操纵杆移动到球体内(任何方向)即使我没有触及球体内部,我该如何处理呢?

P.S:TouchEvent = MotionEvent

到目前为止,这是我最好的代码

公共类控制器{

public float ControllerPosX;
public float ControllerPosY;
public float ArrowPosX, ArrowPosY;
public float CurrentPosX, CurrentPosY;
public double Angle;
public BoundingCircle ControllerCircle, ArrowCircle;
public boolean ControllerLocked, ArrowLocked;

public Controller() {

}

public void Update(List<TouchEvent> touchEvents) {
    int len = touchEvents.size();
    for (int i = 0; i < len; i++) {
        TouchEvent event = (TouchEvent) touchEvents.get(i);
        if (event.type != TouchEvent.TOUCH_UP) {
            if (!ControllerLocked) {
                ControllerPosX = event.x;
                ControllerPosY = event.y;
                ControllerCircle = new BoundingCircle(Assets.Sphere,
                        ControllerPosX, ControllerPosY);
                ControllerLocked = true;
            }
            if (event.type == TouchEvent.TOUCH_DRAGGED) {
                Angle = GetAngle();
                ArrowCircle = new BoundingCircle(Assets.Joystick, event.x,
                        event.y);

                if (BoundingCircle.Intersect(ControllerCircle, ArrowCircle))
                    ArrowLocked = false;
                else
                {
                    ArrowLocked = true;
                }

                if (!ArrowLocked) {
                    ArrowPosX = event.x;
                    ArrowPosY = event.y;
                }
            }
        } else {
            ControllerLocked = false;
        }
    }
}

public double GetAngle() {
    return Math.atan2((double)ArrowPosX, (double)ArrowPosY) *180/Math.PI;
}

} &#39;

0 个答案:

没有答案