checkers应用程序无法使用ontouch和ondraw移动碎片

r7knjye2  于 2021-08-20  发布在  Java
关注(0)|答案(0)|浏览(235)

我正在尝试用java在android studio中制作一个跳棋应用程序。到目前为止,我已经能够使用自定义视图绘制电路板和各个部分,并实现了on-draw方法。我的挣扎是试图移动我画在黑板上的碎片。我有一种方法,可以为选定的块可以移动的可用单元格着色,但我不知道单击这些单元格时如何将块移动到这些单元格。在我的ontouch方法中,我所做的就是设置板的选定行和列,以便我可以突出显示该单元格的可用移动。除此之外,我不知道如何获取下一步的数据,并实际移动我的棋子。我知道我的代码有点邋遢,我仍然需要添加助手方法,因为我重复使用代码,但是代码可以工作,我只是不知道如何移动我画的部分。

public boolean onTouchEvent(MotionEvent event) {
    boolean isValid;

    float x = event.getX();
    float y = event.getY();

    int action = event.getAction();

    if(action == MotionEvent.ACTION_DOWN){
        board.setSelectedRow((int)Math.ceil(y/cellSize));
        board.setSelcectedCol((int)Math.ceil(x/cellSize));
        isValid = true;
    }else{
        isValid = false;
    }

    return isValid;
}

private void colorAvailableCells(Canvas canvas, int row, int col) {
    if (board.getSelectedRow() != -1 && board.getSelcectedCol() != -1) {
        canvas.drawRect(col * cellSize, row * cellSize, (col - 1) * cellSize, (row - 1) * cellSize,
                selectedSquareColorPaint);
        if (board.getBoard()[board.getSelectedRow() - 1][board.getSelcectedCol() - 1] == null) {
            canvas.drawRect(col * cellSize, row * cellSize, (col - 1) * cellSize, (row - 1) * cellSize,
                    selectedSquareColorPaint);
        }else{
            if(playerOneTurn){
                if(board.getBoard()[row-1][col-1].getColor() == playerOnePieceColor){
                    if (board.isLeftCellAvailable(playerOneTurn, board.getSelectedRow() - 1, board.getSelcectedCol() - 1)) {
                        if(board.isJumpable()){
                            canvas.drawRect((col - 3) * cellSize, (row + 2) * cellSize, (col - 2) * cellSize, (row+1) * cellSize,
                                    selectedSqaureMoveColorPaint);
                        }else{
                            canvas.drawRect((col - 2) * cellSize, (row + 1) * cellSize, (col - 1) * cellSize, row * cellSize,
                                    selectedSqaureMoveColorPaint);
                        }
                        if (board.isRightCellAvailable(playerOneTurn, board.getSelectedRow() - 1, board.getSelcectedCol() - 1)) {
                            if(board.isJumpable()){
                                canvas.drawRect((col + 2) * cellSize, (row + 2) * cellSize, (col+1) * cellSize, (row+1) * cellSize,
                                        selectedSqaureMoveColorPaint);
                            }else{
                                canvas.drawRect((col + 1) * cellSize, (row + 1) * cellSize, col * cellSize, row * cellSize,
                                        selectedSqaureMoveColorPaint);
                            }
                        }
                    }else{
                        if (board.isRightCellAvailable(playerOneTurn, board.getSelectedRow() - 1, board.getSelcectedCol() - 1)) {
                            if(board.isJumpable()){
                                canvas.drawRect((col + 2) * cellSize, (row + 2) * cellSize, (col+1) * cellSize, (row+1) * cellSize,
                                        selectedSqaureMoveColorPaint);
                            }else{
                                canvas.drawRect((col + 1) * cellSize, (row + 1) * cellSize, col * cellSize, row * cellSize,
                                        selectedSqaureMoveColorPaint);
                            }
                        }
                    }
                }
            }else{
                if(board.getBoard()[row-1][col-1].getColor() == playerTwoPieceColor){
                    if (board.isLeftCellAvailable(playerOneTurn, board.getSelectedRow() - 1, board.getSelcectedCol() - 1)) {
                        if(board.isJumpable()){
                            canvas.drawRect((col - 3) * cellSize, (row - 3) * cellSize, (col - 2) * cellSize, (row - 2) * cellSize,
                                    selectedSqaureMoveColorPaint);
                        }else{
                            canvas.drawRect((col-2)*cellSize, (row-2)*cellSize, (col-1)*cellSize, (row-1)*cellSize,
                                    selectedSqaureMoveColorPaint);
                        }

                        if (board.isRightCellAvailable(playerOneTurn, board.getSelectedRow() - 1, board.getSelcectedCol() - 1)) {
                            if(board.isJumpable()){
                                canvas.drawRect((col + 2) * cellSize, (row - 3) * cellSize, (col+1) * cellSize, (row-2) * cellSize,
                                        selectedSqaureMoveColorPaint);
                            }else{
                                canvas.drawRect((col+1)*cellSize, (row-2)*cellSize, col*cellSize, (row-1)*cellSize,
                                        selectedSqaureMoveColorPaint);
                            }
                        }
                    }else{
                        if (board.isRightCellAvailable(playerOneTurn, board.getSelectedRow() - 1, board.getSelcectedCol() - 1)) {
                            if(board.isJumpable()){
                                canvas.drawRect((col + 2) * cellSize, (row - 3) * cellSize, (col+1) * cellSize, (row-2) * cellSize,
                                        selectedSqaureMoveColorPaint);
                            }else{
                                canvas.drawRect((col+1)*cellSize, (row-2)*cellSize, col*cellSize, (row-1)*cellSize,
                                        selectedSqaureMoveColorPaint);
                            }
                        }
                    }
                }
            }
        }
    }
    invalidate();
}

checkavailablecell方法正在运行

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题