【问题标题】:How to handle multi touch?如何处理多点触控?
【发布时间】:2014-12-16 22:40:43
【问题描述】:

我正在为 android 制作一个平台游戏,我可以在触摸事件方面使用一些帮助。 这是我的代码:

    public boolean onTouch(MotionEvent e, int scaledX, int scaledY) {
    for (int i = 0; i < object.size(); i++) {
        tempObject = object.get(i);
        if (tempObject.getId() == ObjectId.Player) {

            if (e.getAction() == MotionEvent.ACTION_MOVE) {

                if (moveLeft.contains(scaledX, scaledY)) {
                    tempObject.setMovingLeft(true);
                    tempObject.setMovingRight(false);

                }
                if (moveLeftExit.contains(scaledX, scaledY)
                        && !moveLeft.contains(scaledX, scaledY)) {
                    tempObject.setMovingLeft(false);

                }
                if (moveRight.contains(scaledX, scaledY)) {
                    tempObject.setMovingRight(true);
                    tempObject.setMovingLeft(false);

                }
                if (moveRightExit.contains(scaledX, scaledY)
                        && !moveRight.contains(scaledX, scaledY)) {
                    tempObject.setMovingRight(false);

                }

            }
            if (e.getAction() == MotionEvent.ACTION_UP
                    || e.getAction() == MotionEvent.ACTION_OUTSIDE) {

                if (moveLeft.contains(scaledX, scaledY)) {
                    tempObject.setMovingLeft(false);

                }
                if (moveRight.contains(scaledX, scaledY)) {
                    tempObject.setMovingRight(false);

                }
            }

            if (e.getAction() == MotionEvent.ACTION_DOWN) {

                if (jump.contains(scaledX, scaledY)) {
                    if(tempObject.getVelY() ==0)
                    tempObject.setVelY(-15);
                }
            }

        }
    }
    return true;
}

当我使用一根手指时,一切都很好,如果我触摸 moveRight 矩形,角色会向右移动,当我将手指移开时,他会按预期停止。问题是,如果我在触摸其他按钮的同时触摸一个按钮,它不会对它做出反应。 所以我想我的问题是,我怎样才能修改我的代码,让它对多点触控做出反应?

谢谢!! :)

【问题讨论】:

    标签: java android game-engine


    【解决方案1】:

    这是一个简单的代码。

    public boolean onTouch(View v, MotionEvent event) {
    
        int action = MotionEventCompat.getActionMasked(event);
        int pointerIndex = MotionEventCompat.getActionIndex(event);
        int x = (int)MotionEventCompat.getX(event,pointerIndex);
        int y = (int)MotionEventCompat.getY(event,pointerIndex);
    
        switch(action)
        {
        case MotionEvent.ACTION_DOWN:
            //
            // First finger was touched. Set "pointerIndex" to some value (lets say 0 or -1)
            // Save "pointerIndex" corresponding to this touched object.
            //
            break;
        case MotionEvent.ACTION_POINTER_DOWN:
            //
            // More finger touched when first finger is already touched.
            // Save "pointerIndex" corresponding to this touched object.
            //
            break;
        case MotionEvent.ACTION_MOVE:
            //
            // Finger with "pointerIndex" was moved.
            //
            break;
        case MotionEvent.ACTION_UP:
            //
            // The first touched finger went off.
            //
            break;
        case MotionEvent.ACTION_POINTER_UP:
            //
            // Finger with "pointerIndex" went off (other than first finger)
            //
            break;
        }
        return true;
    }
    

    祝你好运。 :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-17
      • 1970-01-01
      • 2016-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多