Android неправильное поведение обнаружения столкновений с движущимися видами - PullRequest
0 голосов
/ 12 февраля 2020

Привет android Инженеры У меня проблема с моей игрой android. Игра состоит в том, чтобы иметь элементы управления xbox, которые выбирают / перемещают объекты на экране. Идея состоит в том, чтобы перемещать изображение указателя на экране, пока оно не включится. Затем мой нужный объект нажмите некоторую кнопку выбора "x", чтобы перетащить этот объект с изображением указателя и поместить его (мой объект) в указанное c место

его перетаскивание, но с помощью элементов управления не трогайте

проблема в том, что столкновение не работает правильно для всех объектов .. фактически работает для 2 или 3, затем не работает

я использовал следующий код для перемещения изображения указателя

 private fun functionUp() {

    val paramsTop = cursorLayout.layoutParams as RelativeLayout.LayoutParams
    paramsTop.bottomMargin += 60
    cursorLayout.layoutParams = paramsTop
    cursorLayout.invalidate()
    getXY(cursorLayout)
}

private fun functionDown() {
    val paramsBottom = cursorLayout.layoutParams as RelativeLayout.LayoutParams
    paramsBottom.bottomMargin -= 60
    cursorLayout.layoutParams = paramsBottom
    cursorLayout.invalidate()
}

private fun functionLeft() {
    val paramsLeft = cursorLayout.layoutParams as RelativeLayout.LayoutParams
    paramsLeft.leftMargin -= 60
    cursorLayout.layoutParams = paramsLeft
    cursorLayout.invalidate()
}

private fun functionRight() {
    val paramsRight = cursorLayout.layoutParams as RelativeLayout.LayoutParams
    paramsRight.leftMargin += 60
    cursorLayout.layoutParams = paramsRight
    cursorLayout.invalidate()
}

и для обнаружения столкновений

 private fun checkCollision(v1: View, v2: View): Boolean {
    val R1 = Rect(v1.left, v1.top, v1.right, v1.bottom)
    val R2 = Rect(v2.left, v2.top, v2.right, v2.bottom)
    return R1.intersect(R2)
}

и проверки на столкновение

private fun functionSelect() {
        when {
            checkCollision(cursor, wordOne) -> {
                wordOne.backgroundTintList =
                    ContextCompat.getColorStateList(requireContext(), R.color.terquaz)
                if (wordOne.parent != null)
                    (wordOne.parent as ViewGroup).removeView(wordOne)
                cursorLayout.addView(wordOne)
            }
            checkCollision(cursor, wordTwo) -> {
                wordTwo.backgroundTintList =
                    ContextCompat.getColorStateList(requireContext(), R.color.terquaz)
                if (wordTwo.parent != null)
                    (wordTwo.parent as ViewGroup).removeView(wordTwo)
                cursorLayout.addView(wordTwo)
            }
            checkCollision(cursor, wordThree) -> {
                wordThree.backgroundTintList =
                    ContextCompat.getColorStateList(requireContext(), R.color.terquaz)
                if (wordThree.parent != null)
                    (wordThree.parent as ViewGroup).removeView(wordThree)
                cursorLayout.addView(wordThree)

            }
            checkCollision(cursor, wordFour) -> {
                wordFour.backgroundTintList =
                    ContextCompat.getColorStateList(requireContext(), R.color.terquaz)
                if (wordFour.parent != null)
                    (wordFour.parent as ViewGroup).removeView(wordFour)
                cursorLayout.addView(wordFour)
            }
            checkCollision(cursor, wordFive) -> {
                wordFive.backgroundTintList =
                    ContextCompat.getColorStateList(requireContext(), R.color.terquaz)
                if (wordFive.parent != null)
                    (wordFive.parent as ViewGroup).removeView(wordFive)
                cursorLayout.addView(wordFive)
            }
            checkCollision(cursor, wordSix) -> {
                wordSix.backgroundTintList =
                    ContextCompat.getColorStateList(requireContext(), R.color.terquaz)
                if (wordSix.parent != null)
                    (wordSix.parent as ViewGroup).removeView(wordSix)
                cursorLayout.addView(wordSix)
            }
            checkCollision(wordOne, lines) -> {
                if (wordOne.parent != null)
                    (wordOne.parent as ViewGroup).removeView(wordOne)
                targets.addView(wordOne)
                cursorLayout.removeView(wordOne)
                checkResult()
            }
            checkCollision(wordTwo, lines) -> {
                if (wordTwo.parent != null)
                    (wordTwo.parent as ViewGroup).removeView(wordTwo)
                targets.addView(wordTwo)
                cursorLayout.removeView(wordTwo)
                checkResult()
            }
            checkCollision(wordThree, lines[2]) -> {
                if (wordThree.parent != null)
                    (wordThree.parent as ViewGroup).removeView(wordThree)
                targets.addView(wordThree)
                cursorLayout.removeView(wordThree)
                checkResult()
            }
            checkCollision(wordFour, lines[3]) -> {
                if (wordFour.parent != null)
                    (wordFour.parent as ViewGroup).removeView(wordFour)
                targets.addView(wordFour)
                cursorLayout.removeView(wordFour)
                checkResult()
            }
            checkCollision(wordFive, lines[4]) -> {
                if (wordFive.parent != null)
                    (wordFive.parent as ViewGroup).removeView(wordFive)
                targets.addView(wordFive)
                cursorLayout.removeView(wordFive)
                checkResult()
            }
            checkCollision(wordSix, lines[5]) -> {
                if (wordSix.parent != null)
                    (wordSix.parent as ViewGroup).removeView(wordSix)
                targets.addView(wordSix)
                cursorLayout.removeView(wordSix)
                checkResult()
            }
    }
}

мой макет

<RelativeLayout
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal">

        <RelativeLayout
            android:layout_width="100dp"
            android:layout_height="50dp"
            android:background="@drawable/choicebox"
            android:tag="one">

            <TextView
                android:id="@+id/wordOne"
                style="@style/textViewSentence"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:text="أَقَرطُوس" />

        </RelativeLayout>

        <View
            android:layout_width="10dp"
            android:layout_height="0dp" />

        <RelativeLayout
            android:layout_width="100dp"
            android:layout_height="50dp"
            android:background="@drawable/choicebox">

            <TextView
                android:id="@+id/wordTwo"
                style="@style/textViewSentence"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:text="سَلِّم" />

        </RelativeLayout>

        <View
            android:layout_width="10dp"
            android:layout_height="0dp" />

        <RelativeLayout
            android:layout_width="100dp"
            android:layout_height="50dp"
            android:background="@drawable/choicebox">

            <TextView
                android:id="@+id/wordThree"
                style="@style/textViewSentence"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:text="على" />

        </RelativeLayout>

        <View
            android:layout_width="10dp"
            android:layout_height="0dp" />


        <RelativeLayout
            android:layout_width="100dp"
            android:layout_height="50dp"
            android:background="@drawable/choicebox">

            <TextView
                android:id="@+id/wordFour"
                style="@style/textViewSentence"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:text="الأَصْدِقَاءِ" />

        </RelativeLayout>

        <View
            android:layout_width="10dp"
            android:layout_height="0dp" />


        <RelativeLayout
            android:layout_width="100dp"
            android:layout_height="50dp"
            android:background="@drawable/choicebox">

            <TextView
                android:id="@+id/wordFive"
                style="@style/textViewSentence"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:text="لِنَبدَأَ" />

        </RelativeLayout>

        <View
            android:layout_width="10dp"
            android:layout_height="0dp" />

        <RelativeLayout
            android:id="@+id/wordSix"
            android:layout_width="100dp"
            android:layout_height="50dp"
            android:background="@drawable/choicebox">

            <TextView
                style="@style/textViewSentence"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:text="اللُّعبَةَ" />

        </RelativeLayout>

    </LinearLayout>

    <LinearLayout
        android:id="@+id/targets"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:gravity="right"
        android:layoutDirection="rtl"
        android:orientation="horizontal" />

    <LinearLayout
        android:id="@+id/lines"
        android:layout_width="match_parent"
        android:layout_height="20dp"
        android:layout_centerVertical="true"
        android:gravity="right"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/line1"
            android:layout_width="80dp"
            android:layout_height="30dp"
            android:src="@drawable/lines"
            android:tag="one" />

        <View
            android:layout_width="30dp"
            android:layout_height="0dp" />


        <ImageView
            android:id="@+id/line2"
            android:layout_width="80dp"
            android:layout_height="30dp"
            android:src="@drawable/lines"
            android:tag="two" />

        <View
            android:layout_width="30dp"
            android:layout_height="0dp" />

        <ImageView
            android:id="@+id/line3"
            android:layout_width="80dp"
            android:layout_height="30dp"
            android:src="@drawable/lines"
            android:tag="three" />

        <View
            android:layout_width="30dp"
            android:layout_height="0dp" />


        <ImageView
            android:id="@+id/line4"
            android:layout_width="80dp"
            android:layout_height="30dp"
            android:src="@drawable/lines"
            android:tag="four" />

        <View
            android:layout_width="30dp"
            android:layout_height="0dp" />

        <ImageView
            android:id="@+id/line5"
            android:layout_width="80dp"
            android:layout_height="30dp"
            android:src="@drawable/lines"
            android:tag="five" />

        <View
            android:layout_width="30dp"
            android:layout_height="0dp" />


        <ImageView
            android:id="@+id/line6"
            android:layout_width="80dp"
            android:layout_height="30dp"
            android:src="@drawable/lines"
            android:tag="six" />

    </LinearLayout>


    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerInParent="true">

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="200dp"
            android:layout_height="150dp"
            android:layout_marginStart="16dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginBottom="8dp"
            android:src="@drawable/joystick"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.7" />

        <ImageView
            android:id="@+id/selectBtn"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@drawable/controlx"
            app:layout_constraintBottom_toBottomOf="@+id/imageView2"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.833"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.31" />

        <ImageView
            android:id="@+id/imageView5"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@drawable/controldirections"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/selectBtn"
            app:layout_constraintHorizontal_bias="0.48"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="@+id/imageView2"
            app:layout_constraintVertical_bias="0.29000002" />

        <View
            android:id="@+id/rightBt"
            android:layout_width="20dp"
            android:layout_height="10dp"
            app:layout_constraintBottom_toBottomOf="@+id/imageView2"
            app:layout_constraintEnd_toEndOf="@+id/selectBtn"
            app:layout_constraintHorizontal_bias="0.4"
            app:layout_constraintStart_toStartOf="@+id/imageView2"
            app:layout_constraintTop_toTopOf="@+id/imageView5"
            app:layout_constraintVertical_bias="0.17000002" />

        <View
            android:id="@+id/leftBt"
            android:layout_width="20dp"
            android:layout_height="10dp"
            app:layout_constraintBottom_toBottomOf="@+id/imageView5"
            app:layout_constraintEnd_toStartOf="@+id/rightBt"
            app:layout_constraintHorizontal_bias="0.76"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="@+id/imageView5"
            app:layout_constraintVertical_bias="0.47000003" />

        <View
            android:id="@+id/upBt"
            android:layout_width="10dp"
            android:layout_height="20dp"
            app:layout_constraintBottom_toBottomOf="@+id/imageView5"
            app:layout_constraintEnd_toEndOf="@+id/imageView5"
            app:layout_constraintHorizontal_bias="0.54"
            app:layout_constraintStart_toStartOf="@+id/imageView5"
            app:layout_constraintTop_toTopOf="@+id/imageView5"
            app:layout_constraintVertical_bias="0.060000002" />

        <View
            android:id="@+id/downBt"
            android:layout_width="10dp"
            android:layout_height="20dp"
            app:layout_constraintBottom_toBottomOf="@+id/imageView2"
            app:layout_constraintEnd_toEndOf="@+id/rightBt"
            app:layout_constraintHorizontal_bias="0.49"
            app:layout_constraintStart_toStartOf="@+id/imageView5"
            app:layout_constraintTop_toBottomOf="@+id/upBt"
            app:layout_constraintVertical_bias="0.06999999" />

    </androidx.constraintlayout.widget.ConstraintLayout>

    <FrameLayout
        android:id="@+id/cursorLayout"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true">

        <ImageView
            android:id="@+id/cursor"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@drawable/cursor" />
    </FrameLayout>

</RelativeLayout>

спасибо

1 Ответ

0 голосов
/ 21 февраля 2020

Я выяснил проблему ... это было в методе обнаружения столкновений в android, поэтому gitHitRecArea не работал, если представления не в том же макете (и это мое предположение), но когда я использовал view.getGlobalVisibleRect, тогда rect1.intersect(rect2)

это работает как шарм !!

только что опубликовал его на случай, если кто-нибудь столкнется с такой же проблемой

...