How can I separate the screen from two equally large spaces that respond to touch events independently?

At first, for better imagination, this is what the gui looks like. When you touch the right rectangle on the right side of the screen, the right rectangle moves downward. But when both players do it on the side, the ACTION_MOVE fires, which shouldn't be the case in this case ...

enter image description here

I am developing a relatively light multiplayer Android game where two users can interact with the screen. I have implemented onTouch differently depending on which side of the screen was pressed. It then triggers the action of player1 or player2.

My problem is that when both players press the screen at the same time, the ACTION_MOVE is fired as well, not just the two ACTION_DOWN events. ACTION_MOVE has a definite implementation, but it should only be called when one player by themselves calls it, not both together.

So, is there a way to split the screen into two so that the described problem does not occur?

thank

+3


source to share


1 answer


You should implement a LinearLayout with two buttons with weight = 1 for each.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
    android:id="@+id/button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:background="@color/colorPrimary"
    android:text="Button1" />

<Button
    android:id="@+id/button2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:background="@color/colorAccent"
    android:text="Button2" />
</LinearLayout>

      



Then you can process strokes from each button.

+1


source







All Articles