当手指停止移动时,Action.Move不起作用

时间:2014-11-14 13:29:29

标签: java android multi-touch ontouchevent

这实际上是我第一次在这里提问,所以请耐心等待我:)

我需要在不使用任何外部库的情况下为大学制作安卓游戏,我遇到以下问题:

在游戏中,您可以通过在屏幕两侧上下滑动来控制太空中的出租车(横向模式),其中顶部是最大速度,底部是慢速,右侧是您控制右侧推进器并且打开左推进器左侧。基本上控制已经在运行,有一个问题: 如果你停止移动手指(手指仍在触摸屏幕),则不再发生任何事情(大约1秒后):

这是我第一次编程Android,请记住(如果我犯了一个非常明显的错误):

@Override
public boolean onTouchEvent(MotionEvent event) {

    int maskedAction = event.getActionMasked();

    switch (maskedAction) {

    case MotionEvent.ACTION_DOWN:
    case MotionEvent.ACTION_POINTER_DOWN:
    {
        // not needed
        break;
    }

    case MotionEvent.ACTION_MOVE:
    {
        // all Settings are set here

        // normalized in [0,1], will be given to GameEngine Acceleration methods
        float leftThrusterNormalized = 0.0f;
        float rightThrusterNormalized = 0.0f;

        // counter for the current pointer (= TouchEvent) 
        int current = 0;

        // true = more than one pointer; false = one or no pointers
        boolean multiTouch = false;

        // go through each pointer 
        for (int i = 0; i < event.getPointerCount(); i++) {

            // if there are more than two pointers, this is needed, otherwise an 
            // java.lang.IllegalArgumentException will be thrown if only pointer is active 
            if (event.getPointerCount() >= 2) {
                multiTouch = true;
                current = event.getPointerId(i);

                // the touch event occurs in the right area, relative to screen width 
                if (event.getX(current) < getWidth()*0.2 || event.getX(current) > getWidth()*0.8)  {

                    engine.accelerateTaxiTranslation(normalizeCoords(event.getY(current)));

                    // Touch event on the right side of the screen, set value for the right Thruster
                    // via normalisation of y-Coordinate
                    if (event.getX(current) > getWidth() * 0.8) {
                        rightThrusterNormalized = normalizeCoords(event.getY(current));
                    } 

                    // Touch event on the left side of the screen, set value for the left Thruster
                    // via normalisation of y-Coordinate
                    if (event.getX(current) < getWidth() * 0.2) {
                        leftThrusterNormalized = normalizeCoords(event.getY(current));                  
                    }
                }
            } 

            // only one pointer, therefore only one thruster at a time will be active, 
            // ==> only setting Rotation Speed, not Translation Speed
            else if (event.getPointerCount() == 1) {

                // turn left
                if (event.getX() > getWidth() * 0.8) {
                    engine.accelerateTaxiRotation(-1*(normalizeCoords(event.getY())));
                } 

                // turn right
                else if (event.getX() < getWidth() * 0.2) {
                    engine.accelerateTaxiRotation(normalizeCoords(event.getY()));
                }
            }
        }

        // in case of multiple pointers the rotation value is set via the difference of 
        // bigger - smaller value (difference of the different touch events)
        if (multiTouch) {

            // right Thruster is stronger than left thruster ==> left turn and therefore multiply with -1
            if (leftThrusterNormalized < rightThrusterNormalized) {
                engine.accelerateTaxiRotation(-1*(rightThrusterNormalized-leftThrusterNormalized));
            }
            // left Thruster is stronger than right thruster ==> right turn and therefore positive value
            else if (leftThrusterNormalized >= rightThrusterNormalized) {
                engine.accelerateTaxiRotation(leftThrusterNormalized - rightThrusterNormalized);
            }
        }
        break;
    }

    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_POINTER_UP:
    {
        // not needed
        break;
    }

    }
    // return true, otherwise MotionEvent.ACTION_MOVE will not be detected
    return true;
}

我是否必须在ACTION.DOWN中编写整个代码?或者花时间参加那个活动?帮助将不胜感激。

当手指不动时,我期望它做什么: (当前行为)根据手指的y位置,插入[0,1]之间的值并将其提供给翻译方法,当我不移动手指时,我希望出租车保持速度可以这么说。现在,即使我注释了ACTION_DOWN,ACTION_POINTER_DOWN等,如user3427079建议的那样,船停了,但是如果手指停止移动但仍然按在屏幕上,我希望它以恒定速度继续。

0 个答案:

没有答案