Search code examples
javaandroidontouch

ACTION_MOVE returns the same value every time it is called in OnTouch


I'm trying to call a method to update a tts element with a different value every time the ACTION_MOVE is called. However, it returns the same value every time it is called. The respondCoordinates(v) method has an if statement to change the value.

public boolean onTouch(View v, MotionEvent event) {
    //_gestureDetector.onTouchEvent(event);

    int eventAction = event.getAction();

    // you may need the x/y location
    int x = (int)event.getRawX();
    int y = (int)event.getRawY();

    Point boardPos = getBoardIndexX(new Point(x,y));
    // put your code in here to handle the event
    switch (eventAction) {
        case MotionEvent.ACTION_DOWN:

            Log.d("Chess", "Down: " + x + ", " + y + " [" + boardPos.x + ", " + boardPos.y + "]");
            if(OnBoard(boardPos))
            {
                // then play
                lastPlayTime = System.currentTimeMillis();
                lastPlayPos.x = boardPos.x;
                lastPlayPos.y = boardPos.y;
                respondCoordinates(v);

            }

            break;//return true;
        case MotionEvent.ACTION_UP:
            Log.d("Chess", "Up: " + x + ", " + y + " [" + boardPos.x + ", " + boardPos.y + "]");
            lastPlayPos.x = -1;
            lastPlayPos.y = -1;

            v.performClick();
            break;//return true;
        case MotionEvent.ACTION_MOVE:
            if(boardPos.x != lastPlayPos.x || boardPos.y != lastPlayPos.y) {
                // then we're on a new square
                Log.d("Chess", "Move: " + x + ", " + y + " [" + boardPos.x + ", " + boardPos.y + "]");
                lastPlayPos.x = boardPos.x;
                lastPlayPos.y = boardPos.y;

                if(OnBoard(boardPos))
                {
                    // then play
                    lastPlayTime = System.currentTimeMillis();
                    respondCoordinates(v);
                }
            }
    }
    // tell the View to redraw the Canvas
    //invalidate();

    return true;//super.onTouchEvent(event);
}

public void respondCoordinates(View v) {

    int iTo = getIndexOfButton(v);

    String join = "";
    String letter = "";
    String number = "";
    String piece = "";


    if ((iTo >= 0) && (iTo <= 7)) {
        if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.PAWN || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.PAWN) {
            piece = "Pawn";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.BISHOP || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.BISHOP) {
            piece = "Bishop";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.ROOK || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.ROOK) {
            piece = "Rook";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.KNIGHT || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.KNIGHT) {
            piece = "Knight";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.QUEEN || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.QUEEN) {
            piece = "Queen";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.KING || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.KING) {
            piece = "King";
        }
        number = "8";
        if (iTo == 0) {
            letter = "a";


        } else if (iTo == 1) {
            letter = "b";
        } else if (iTo == 2) {
            letter = "c";
        } else if (iTo == 3) {
            letter = "d";

        } else if (iTo == 4) {
            letter = "e";

        } else if (iTo == 5) {
            letter = "f";

        } else if (iTo == 6) {
            letter = "g";

        } else if (iTo == 7) {
            letter = "h";

        }

    } else if ((iTo >= 8) && (iTo <= 15)) {
        number = "7";
        if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.PAWN || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.PAWN) {
            piece = "Pawn";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.BISHOP || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.BISHOP) {
            piece = "Bishop";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.ROOK || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.ROOK) {
            piece = "Rook";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.KNIGHT || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.KNIGHT) {
            piece = "Knight";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.QUEEN || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.QUEEN) {
            piece = "Queen";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.KING || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.KING) {
            piece = "King";
        }

        if (iTo == 8) {
            letter = "a";
        } else if (iTo == 9) {
            letter = "b";
        } else if (iTo == 10) {
            letter = "c";
        } else if (iTo == 11) {
            letter = "d";
        } else if (iTo == 12) {
            letter = "e";
        } else if (iTo == 13) {
            letter = "f";
        } else if (iTo == 14) {
            letter = "g";
        } else if (iTo == 15) {
            letter = "h";
        }


    } else if ((iTo >= 16) && (iTo <= 23)) {
        number = "6";
        if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.PAWN || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.PAWN) {
            piece = "Pawn";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.BISHOP || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.BISHOP) {
            piece = "Bishop";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.ROOK || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.ROOK) {
            piece = "Rook";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.KNIGHT || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.KNIGHT) {
            piece = "Knight";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.QUEEN || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.QUEEN) {
            piece = "Queen";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.KING || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.KING) {
            piece = "King";
        }

        if (iTo == 16) {
            letter = "a";
        } else if (iTo == 17) {
            letter = "b";
        } else if (iTo == 18) {
            letter = "c";
        } else if (iTo == 19) {
            letter = "d";
        } else if (iTo == 20) {
            letter = "e";
        } else if (iTo == 21) {
            letter = "f";
        } else if (iTo == 22) {
            letter = "g";
        } else if (iTo == 23) {
            letter = "h";
        }


    } else if ((iTo >= 24) && (iTo <= 31)) {
        number = "5";
        if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.PAWN || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.PAWN) {
            piece = "Pawn";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.BISHOP || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.BISHOP) {
            piece = "Bishop";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.ROOK || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.ROOK) {
            piece = "Rook";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.KNIGHT || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.KNIGHT) {
            piece = "Knight";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.QUEEN || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.QUEEN) {
            piece = "Queen";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.KING || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.KING) {
            piece = "King";
        }

        if (iTo == 24) {
            letter = "a";
        } else if (iTo == 25) {
            letter = "b";
        } else if (iTo == 26) {
            letter = "c";
        } else if (iTo == 27) {
            letter = "d";
        } else if (iTo == 28) {
            letter = "e";
        } else if (iTo == 29) {
            letter = "f";
        } else if (iTo == 30) {
            letter = "g";
        } else if (iTo == 31) {
            letter = "h";
        }


    } else if ((iTo >= 32) && (iTo <= 39)) {
        number = "4";
        if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.PAWN || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.PAWN) {
            piece = "Pawn";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.BISHOP || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.BISHOP) {
            piece = "Bishop";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.ROOK || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.ROOK) {
            piece = "Rook";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.KNIGHT || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.KNIGHT) {
            piece = "Knight";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.QUEEN || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.QUEEN) {
            piece = "Queen";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.KING || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.KING) {
            piece = "King";
        }

        if (iTo == 32) {
            letter = "a";
        } else if (iTo == 33) {
            letter = "b";
        } else if (iTo == 34) {
            letter = "c";
        } else if (iTo == 35) {
            letter = "d";
        } else if (iTo == 36) {
            letter = "e";
        } else if (iTo == 37) {
            letter = "f";
        } else if (iTo == 38) {
            letter = "g";
        } else if (iTo == 39) {
            letter = "h";
        }


    } else if ((iTo >= 40) && (iTo <= 47)) {
        number = "3";
        if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.PAWN || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.PAWN) {
            piece = "Pawn";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.BISHOP || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.BISHOP) {
            piece = "Bishop";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.ROOK || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.ROOK) {
            piece = "Rook";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.KNIGHT || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.KNIGHT) {
            piece = "Knight";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.QUEEN || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.QUEEN) {
            piece = "Queen";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.KING || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.KING) {
            piece = "King";
        }

        if (iTo == 40) {
            letter = "a";
        } else if (iTo == 41) {
            letter = "b";
        } else if (iTo == 42) {
            letter = "c";
        } else if (iTo == 43) {
            letter = "d";
        } else if (iTo == 44) {
            letter = "e";
        } else if (iTo == 45) {
            letter = "f";
        } else if (iTo == 46) {
            letter = "g";
        } else if (iTo == 47) {
            letter = "h";
        }


    } else if ((iTo >= 48) && (iTo <= 55)) {
        number = "2";
        if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.PAWN || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.PAWN) {
            piece = "Pawn";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.BISHOP || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.BISHOP) {
            piece = "Bishop";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.ROOK || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.ROOK) {
            piece = "Rook";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.KNIGHT || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.KNIGHT) {
            piece = "Knight";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.QUEEN || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.QUEEN) {
            piece = "Queen";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.KING || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.KING) {
            piece = "King";
        }

        if (iTo == 48) {
            letter = "a";
        } else if (iTo == 49) {
            letter = "b";
        } else if (iTo == 50) {
            letter = "c";
        } else if (iTo == 51) {
            letter = "d";
        } else if (iTo == 52) {
            letter = "e";
        } else if (iTo == 53) {
            letter = "f";
        } else if (iTo == 54) {
            letter = "g";
        } else if (iTo == 55) {
            letter = "h";
        }


    } else if ((iTo >= 56) && (iTo <= 63)) {
        number = "1";
        if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.PAWN || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.PAWN) {
            piece = "Pawn";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.BISHOP || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.BISHOP) {
            piece = "Bishop";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.ROOK || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.ROOK) {
            piece = "Rook";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.KNIGHT || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.KNIGHT) {
            piece = "Knight";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.QUEEN || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.QUEEN) {
            piece = "Queen";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.KING || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.KING) {
            piece = "King";
        }

        if (iTo == 56) {
            letter = "a";
        } else if (iTo == 57) {
            letter = "b";
        } else if (iTo == 58) {
            letter = "c";
        } else if (iTo == 59) {
            letter = "d";
        } else if (iTo == 60) {
            letter = "e";
        } else if (iTo == 61) {
            letter = "f";
        } else if (iTo == 62) {
            letter = "g";
        } else if (iTo == 63) {
            letter = "h";
        }
    }

    join = letter + number + "" + piece;

    //Log.d("Chess", join);

    _chessActivity.soundNotification(join);


}


public int getIndexOfButton(View but){
    for(int i = 0; i < 64; i++){
        if(_arrImages[i] == ((ChessImageView)but)){

            _arrImages[i].setPressed(false);
            return i;
        }
    }
    return -1;
}

Solution

  • I think I understand the issue correctly. You are assuming that the View passed to onTouch is one of the 64 views on the chessboard, but this is not the case. The View passed to onTouch is the View that you set the OnTouchListener on. Which, in this case, is the chessboard itself.

    Instead, you should be finding the index of the individual piece by using the coordinates of the MotionEvent. The method should look something like this:

    private int mLastChildIndex = -1;
    
    public boolean onTouch(View view, MotionEvent event) {
        int x = (int) event.getRawX();
        int y = (int) event.getRawY();
    
        switch(event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mLastChildIndex = getChildIndex((ViewGroup) view, x, y);
                playNotificationForIndex(mLastChildIndex);
                break;
            case MotionEvent.ACTION_MOVE:
                int currentIndex = getChildIndex((ViewGroup) view, x, y);
                if (currentIndex != mLastChildIndex) {
                    playNotificationForIndex(currentIndex);
                    mLastChildIndex = currentIndex;
                } break;
            case MotionEvent.ACTION_UP:
                performClickForIndex(mLastChildIndex);
                mLastChildIndex = -1;
                break;
        }
    
        return true; // This method eats all MotionEvent actions for the View
    }
    
    private int getChildIndex(ViewGroup viewGroup, int x, int y) {
        for (int i = 0; i < viewGroup.getChildCount(); i++) {
            View child = viewGroup.getChildAt(i);
    
            int[] coordinates = new int[2];
            child.getLocationOnScreen(coordinates);
    
            Rect bounds = new Rect(coordinates[0], coordinates[1],
                    coordinates[0] + child.getWidth(), coordinates[1] + child.getHeight());
            if (bounds.contains(x, y)) {
                return i;
            }
        } return -1;
    }