3

At first for better imagination, this is what the gui looks like. When you touch below the right rectangle on the right side of the screen, the right rectanglemoves downwards. But when both players do this on there side, there is a ACTION_MOVE triggered which should not be the case in this scenario...

enter image description here

I am developing a relatively easy multiplayer game in android where two users can interact with the screen. I implemented onTouch differently, depending on the fact which side of the screen has been pressed. It then triggers either an action of player1 or player 2.

My problem is,that when both players press the screen simultaneously, there is a ACTION_MOVE triggered also, and not only two ACTION_DOWN events. The ACTION_MOVE has some specific implementation, but it should only be called when one player on his own causes it and not both together.

So, is there a way to separate the screen into to two areas so that the described problem will not arise?

Thanks

Jonas1902
  • 93
  • 1
  • 10

1 Answers1

1

You should implemet 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 handle touches from each buttons.

Giacomo Lai
  • 494
  • 3
  • 15
  • Yes what you say is of course correct! But I am using a custom view and want to draw on a canvas and there shouldn't be any buttons. If I use buttons there I could not handle any ACTION_MOVE which is necessary for my program. But ACTION_MOVE shall only be triggered by one single user and not as a result of two single taps, one by each user. – Jonas1902 Jul 01 '17 at 06:55