I make a 2d game. When I touch a column , 2 or 3 cell is changing, not 1.
If I keep touch on the screen, onTouch method work until I lift my finger. But ı don't want this, I Want onTouch method to work once per touch. How can I do this?
This is a part of my code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_customer);
MyView a = new MyView(this);
setContentView(a);
a.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View view, MotionEvent motionEvent) {
float mX = motionEvent.getX();
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (board[0][j].getterCellType() == '.') {
if (mX > board[i][j].getterxPos()-50 && mX < board[i][j].getterxPos() + 50) {
playColumn = board[i][j].getterColumn();
play();
return true;
}
}
}
}
return true;
}
});
}
If you want to only execute once, then you shoud detect ACTION_DOWN
. The onTouch
method will be called in every MotionEvent.ACTION_MOVE
, MotionEvent.ACTION_DOWN
, and MotionEvent.UP
.
public boolean onTouch(View view, MotionEvent motionEvent) {
if(motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
//code code code
}
}