touchlistener的代码出错 - Android

时间:2013-03-14 20:38:15

标签: java android tic-tac-toe

我是Android的新手,在线教程后,我在使用TouchListener的代码块时遇到了麻烦。

我不断得到的错误是:

运营商&&未定义参数类型boolean,void

以下代码位于MainActivity.java中。我在以下星号突出显示的行上显示此错误:

    // Listen for touches on the board
private OnTouchListener mTouchListener = new OnTouchListener() { 
public boolean onTouch(View v, MotionEvent event) { 
// Determine which cell was touched 
int col = (int) event.getX() / mBoardView.getBoardCellWidth(); 
int row = (int) event.getY() / mBoardView.getBoardCellHeight(); 
int pos = row * 3 + col; 
    if (!mGameOver && setMove(TicTacToeGame.HUMAN_PLAYER, pos)) { //*****************


    // If no winner yet, let the computer make a move
    int winner = mGame.checkForWinner();
    if (winner == 0) { 
        mInfoTextView.setText(R.string.turn_computer); 

        setMove(TicTacToeGame.COMPUTER_PLAYER, pos);
        winner = mGame.checkForWinner();
    }   

 } 
return false; 
 } 
};

我认为这是因为在TicTacToeGame.java中setMove()是无效的:

public void setMove(char player, int location) {
    if (location >= 0 && location < BOARD_SIZE &&
        mBoard[location] == OPEN_SPOT) {
        mBoard[location] = player;

    }

}

我已经严格遵循了教程http://www.harding.edu/fmccown/classes/comp475-s10/tic-tac-toe-graphics.pdf

我将非常感谢任何帮助。

非常感谢,

Beth Ann

1 个答案:

答案 0 :(得分:2)

在您链接到的PDF中,setMove()有一个布尔返回类型(第5页,顶部):

private boolean setMove(char player, int location) { 
    if (mGame.setMove(player, location)) { 
        mBoardView.invalidate(); // Redraw the board
        if (player == TicTacToeGame.HUMAN_PLAYER) 
            mHumanMediaPlayer.start(); 
        else
            mComputerMediaPlayer.start(); 
        return true; 
    } 
    return false; 
}