Reputation: 117
This is my first game app. I've built a card game, players can tap or drag a card. I've noticed that when a player taps a card, it appears tha the onTouch method fires multiple times. My basic code is as such:
cardView.setOnTouchListener(new View.OnTouchListener() {
Log.d(TAG, "Card is Touched");
public boolean onTouch(View v, MotionEvent event) {
moveCardMethod();
}
}
The method:
private void moveCardMethod() {
Log.d(TAG, "Card Move method has started.");
case MotionEvent.ACTION_DOWN:
Log.d(TAG, "Action down has started.");
break;
case MotionEvent.ACTION_MOVE:
Log.d(TAG, "Action move has started.");
break;
case MotionEvent.ACTION_UP:
Log.d(TAG, "Action up has started.");
break;
Log.d(TAG, "The end.");
}
Results:
Am I doing something incorrect? I would assume that when the card is tapped (or dragged), it would results in something like this:
The code appears to work during the game, but this concerns me. From the log, it appears with a tap or drag "moveCardMethod()" is called multiple times.
I didn't want to move over complicate the example, but I have tried to use a isCardBusy boolean to ignore the method if isCardBusy = true. However, it really didn't work. Plus, it causes problems during the action move portion.
Since this is my first app, I am not confident I have implemented the onTouch correctly.
Upvotes: 0
Views: 119
Reputation: 192
Yep you implemented it incorrectly, you should return true/false in the onTouch(..) function + add the drag code when the event.getAction() is ACTION_DOWN.
cardView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(
view);
view.startDrag(null, shadowBuilder, view, 0);
view.setVisibility(View.INVISIBLE);
return true;
} else {
return false;
}
}
});
Upvotes: 0