I am making a game. I use GridView
to display game blocks of various shapes ( tetris-like). So I have for example L block, T block etc.
When player touch and press on a block, he can move it anywhere on the screen. Each block is actualy a GridView, which has few cells colored and few invisible (depends on the block shape).
The problem occurs, when the blocks are near to each other, and you want to move a block which is "under" other blocks. Because of the gridView shape, touching at 1 or 2 position of the green block will not trigger green block touch listener, but blue (since the blue block was placed after the green on the layout, and you are touching inside its boundaries). So the player will start moving the blue block, but of course he wanted to move the green.
How can I fix this problem?
Edit:
I tried to set touch listener to every Blocks gridView
child (don't mind empty cells at this moment), and pass the information to the methods I made. However, ACTION_DOWN
works, but once I move my finger, the ACTION_UP
is triggered (without any ACTION_MOVE
).
void setBlockTouchListener(final Block block)
{
for (int i = 0; i < block.getChildCount(); i++)
{
block.getChildAt(i).setOnTouchListener(new View.OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
final int X = (int) event.getRawX();
final int Y = (int) event.getRawY();
switch (event.getAction() & MotionEvent.ACTION_MASK)
{
case MotionEvent.ACTION_DOWN:
Log.d("TAG", "child down");
onDown(block, X, Y, MotionEvent.ACTION_DOWN);
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
Log.d("TAG", "child cancel");
onCancel(block, X, Y, MotionEvent.ACTION_CANCEL);
break;
case MotionEvent.ACTION_MOVE:
Log.d("TAG", "child move");
onMove(block, X, Y, MotionEvent.ACTION_MOVE);
break;
}
return true;
}
});
}
}
Logcat (when I try to touch and move the block):
child_down
child_cancel