Home > database >  Android detect tap or move - for chess piece interaction
Android detect tap or move - for chess piece interaction

Time:10-07

I want to implement touch listener that detects

  1. Click/tap on chess piece in order to select ot
  2. Swipe/move of chess piece immediately.

How to do it?

CodePudding user response:

Unfortunately, I am not sure what you mean by a "chess piece", but let's assume that the board is a view in the background, and inside you have inner views which are chess pieces.

  1. To achieve the select/unselect given piece effect you should add a simple View.OnClickListener interface to your view. Each of your pieces should have a view class and some data connected to it (like a position, a type of piece, id, etc.). After clicking the piece update the state of some global variable, for example, selectedPiece with an ID assigned to a given piece. You should also change the UI of a given piece, for example change the border to show the selected piece to a user. Then after clicking a new field for moving the piece you should update the UI by moving your piece view to an appropriate position. Also, the data of a piece or a game should be updated - depending on the place of storing the pieces' positions.
  2. For this you should implement the drag&drop feature which is described here: https://www.tutorialspoint.com/android/android_drag_and_drop.html. You should determine the places where the user can move a piece and in case of dropping the piece in the wrong place, cancel the move and go with the piece to a start position.

CodePudding user response:

Basic response to touch events using event.x and event.y works:

https://developer.android.com/develop/ui/views/graphics/opengl/touch

https://suragch.medium.com/how-touch-events-are-delivered-in-android-eee3b607b038

...more info: Android Touch Listener

  • Related