每次调用ACTION_MOVE
时,我都试图调用一个方法来更新一个具有不同值的tts元素。但是,每次调用它时都会返回相同的值。 respondCoordinates(v)
方法有一个if
语句来更改值。
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;
}
答案 0 :(得分:0)
需要检查,但我相信如果你在ACTION_UP之前留下一个视图,你仍然可以获得该视图的ACTION_MOVE,所以你的视图v总是你的ACTION_DOWN开启,即使你移动另一个之一。
这可能是预期的行为。
我认为this可能会证实这一点。他们的解决方案可能不适合您的情况,因为您可以获得所有视图的输出,而不仅仅是当前视图。
这很烦人,但我认为解决方案是在网格上有一个监听器,而不是在单元格上,并找到从x和y触摸的单元格。
答案 1 :(得分:0)
我想我正确理解了这个问题。您假设传递给View
的{{1}}是棋盘上的64个视图之一,但事实并非如此。传递给onTouch
的{{1}}是您设置View
的{{1}}。在这种情况下,棋盘本身也是如此。
相反,您应该使用onTouch
的坐标来查找单个部分的索引。该方法应如下所示:
View