1
\$\begingroup\$

I'm trying to implement multitouch in my game but I'm running into problems. Basically all I want is for the player to run and shoot or jump at the same time. I've followed a multitouch tutorial and integrated it into my game but I'm still not getting the desired result. I have a button class that checks whether a touch event is within certain bounds. In the code, the logging shows that it's working alright and multiple fingers are detected for down and up. But as far as the buttons go only the first button pressed registers. I'd appreciate if someone could help me figure out what's wrong/missing in the code:

@Override
public boolean onTouch(MotionEvent e, int scaledX, int scaledY) {

    int action = e.getAction() & MotionEvent.ACTION_MASK;

    jumpButton.onTouchDown(scaledX, scaledY);
    actionButton.onTouchDown(scaledX, scaledY);
    rightButton.onTouchDown(scaledX, scaledY);
    leftButton.onTouchDown(scaledX, scaledY);
    pauseButton.onTouchDown(scaledX, scaledY);

    switch (action) {
    case MotionEvent.ACTION_DOWN: {
        int id = e.getPointerId(0);
        Log.d("Play", "Pointer Down  [" + id + "]");

        if (gamePaused) {
            gamePaused = false;
        }

        if (jumpButton.isPressed(scaledX, scaledY)) {
            player.jump();
        } else if (actionButton.isPressed(scaledX, scaledY)) {
            Log.d("Play", "Action!");
        } else if (rightButton.isPressed(scaledX, scaledY)) {
            player.moveRight();
        } else if (leftButton.isPressed(scaledX, scaledY)) {
            player.moveLeft();
        } else if (pauseButton.isPressed(scaledX, scaledY)) {
            onPause();
        }
        break;
    }
    case MotionEvent.ACTION_POINTER_DOWN: {
        int id = e.getPointerId(getIndex(e));
        Log.d("Play", "Other pointer down [" + id + "]");

        if (jumpButton.isPressed(scaledX, scaledY)) {
            player.jump();
        } else if (actionButton.isPressed(scaledX, scaledY)) {
            Log.d("Play", "Action!");
        } else if (rightButton.isPressed(scaledX, scaledY)) {
            player.moveRight();
        } else if (leftButton.isPressed(scaledX, scaledY)) {
            player.moveLeft();
        } else if (pauseButton.isPressed(scaledX, scaledY)) {
            onPause();
        }
        break;
    }
    case MotionEvent.ACTION_UP: {
        int id = e.getPointerId(0);
        Log.d("Play", "Pointer up [" + id + "]");
        if (jumpButton.isPressed(scaledX, scaledY)) {
            jumpButton.cancel();
        } else if (actionButton.isPressed(scaledX, scaledY)) {
            actionButton.cancel();
        } else if (rightButton.isPressed(scaledX, scaledY)) {
            player.stop();
            Tile.stop();
            rightButton.cancel();
        } else if (leftButton.isPressed(scaledX, scaledY)) {
            player.stop();
            Tile.stop();
            leftButton.cancel();
        } else if (pauseButton.isPressed(scaledX, scaledY)) {
            pauseButton.cancel();
        } else {
            player.stop();
            Tile.stop();
            jumpButton.cancel();
            actionButton.cancel();
            rightButton.cancel();
            leftButton.cancel();
            pauseButton.cancel();
        }
        break;
    }
    case MotionEvent.ACTION_POINTER_UP: {
        int id = e.getPointerId(getIndex(e));
        Log.d("Play", "Other pointer up [" + id + "]");
        if (jumpButton.isPressed(scaledX, scaledY)) {
            jumpButton.cancel();
        } else if (actionButton.isPressed(scaledX, scaledY)) {
            actionButton.cancel();
        } else if (rightButton.isPressed(scaledX, scaledY)) {
            player.stop();
            Tile.stop();
            rightButton.cancel();
        } else if (leftButton.isPressed(scaledX, scaledY)) {
            player.stop();
            Tile.stop();
            leftButton.cancel();
        } else if (pauseButton.isPressed(scaledX, scaledY)) {
            pauseButton.cancel();
        } else {
            player.stop();
            Tile.stop();
            jumpButton.cancel();
            actionButton.cancel();
            rightButton.cancel();
            leftButton.cancel();
            pauseButton.cancel();
        }
        break;
    }
    }
    return true;
}

private int getIndex(MotionEvent e) {

    int idx = (e.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
    return idx;
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.