简体   繁体   English

touchlistener的代码出错 - Android

[英]Error on code for touchlistener - Android

I am a newbie here at Android, I am having trouble with this code block for touchListener for a TicTacToe game following an online tutorial. 我是Android的新手,我在使用在线教程后的TicTacToe游戏的touchListener代码块时遇到了问题。

The error I keep getting is: 我一直得到的错误是:

The operator && is undefined for the argument type(s) boolean, void 对于参数类型boolean,void,运算符&&未定义

The following code is located in the MainActivity.java. 以下代码位于MainActivity.java中。 I get this error on the line highlighted with stars below: 我在下面用星号突出显示的行上出现此错误:

    // 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; 
 } 
};

I think this is because the setMove() is void in the TicTacToeGame.java: 我认为这是因为setMove()在TicTacToeGame.java中是void的:

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

    }

}

I have followed the tutorial precisely http://www.harding.edu/fmccown/classes/comp475-s10/tic-tac-toe-graphics.pdf 我已经完全按照教程http://www.harding.edu/fmccown/classes/comp475-s10/tic-tac-toe-graphics.pdf

I would be very grateful of any help. 我会非常感谢任何帮助。

Many thanks, 非常感谢,

Beth Ann 贝丝安

In the PDF you linked to, setMove() has a boolean return type (Page 5, top): 在链接到的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; 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM