0

画面に 2 つのソフト方向パッドがあるアプリを作成しています。すべての ACTION_MOVE イベントは id が 0 に等しいという事実があるため、2 つの同時タッチのすべてのタッチ イベントをキャプチャして理解しようとするのは非常に困難です。

私が持っているのは、タッチがあるときに衝突検出システムにパイプされる 2 つの「タッチ」オブジェクトです。私は 2 つの方向パッドを持っており、両方が同時に制御できる必要があることを思い出してください。

誰にも提案はありますか。これが私の onTouchEvent メソッドです。方向パッドとの衝突検出にカスタムの「タッチ」ゲームオブジェクトを使用しているため、コンテキストを理解するのが難しい場合があることに注意してください。

@Override
public void onTouchEvent(MotionEvent e)
{   
    int index = e.getActionIndex();


    int action = MotionEventCompat.getActionMasked(e);
    // Get the index of the pointer associated with the action.
    //index = MotionEventCompat.getActionIndex(e);

    int id = e.getPointerId(index);


    if(touch1.pointerID<0)
        touch1.pointerID = id;
    else
        touch2.pointerID = id;

    switch(action)
    {
        case MotionEvent.ACTION_DOWN:
        {
            if(id == touch1.pointerID)
            {
                touch1.setCollideable(true);
                touch1.getPosition().x = e.getX(index)*GameWorldProperties.getWidthRatio();
                touch1.getPosition().y = e.getY(index)*GameWorldProperties.getHeightRatio();
            }
            else if(id==touch2.pointerID)
            {
                touch2.setCollideable(true);
                touch2.getPosition().x = e.getX(index)*GameWorldProperties.getWidthRatio();
                touch2.getPosition().y = e.getY(index)*GameWorldProperties.getHeightRatio();

            }

            break;  
        }
        case MotionEvent.ACTION_POINTER_DOWN:
        {
            if(id == touch1.pointerID)
            {
                touch1.setCollideable(true);
                touch1.getPosition().x = e.getX(index)*GameWorldProperties.getWidthRatio();
                touch1.getPosition().y = e.getY(index)*GameWorldProperties.getHeightRatio();
            }
            else if(id==touch2.pointerID)
            {
                touch2.setCollideable(true);
                touch2.getPosition().x = e.getX(index)*GameWorldProperties.getWidthRatio();
                touch2.getPosition().y = e.getY(index)*GameWorldProperties.getHeightRatio();

            }
            break;
        }
        case MotionEvent.ACTION_UP:
        {
            if(id == touch1.pointerID)
            {
                touch1.setCollideable(false);
                touch1.pointerID = -1;
                touch1.getPosition().x = e.getX(index)*GameWorldProperties.getWidthRatio();
                touch1.getPosition().y = e.getY(index)*GameWorldProperties.getHeightRatio();
            }
            else if(id==touch2.pointerID)
            {
                touch2.setCollideable(false);
                touch2.pointerID = -1;
                touch2.getPosition().x = e.getX(index)*GameWorldProperties.getWidthRatio();
                touch2.getPosition().y = e.getY(index)*GameWorldProperties.getHeightRatio();

            }
            break;
        }
        case MotionEvent.ACTION_POINTER_UP:
        {
            if(id == touch1.pointerID)
            {
                touch1.setCollideable(false);
                touch1.pointerID = -1;
                touch1.getPosition().x = e.getX(index)*GameWorldProperties.getWidthRatio();
                touch1.getPosition().y = e.getY(index)*GameWorldProperties.getHeightRatio();
            }
            else if(id==touch2.pointerID)
            {
                touch2.setCollideable(false);
                touch2.pointerID = -1;
                touch2.getPosition().x = e.getX(index)*GameWorldProperties.getWidthRatio();
                touch2.getPosition().y = e.getY(index)*GameWorldProperties.getHeightRatio();

            }
            break;
        }
        case MotionEvent.ACTION_MOVE:
        {
                Log.d("pointer",String.format("Move index=%s id=%s", index,id));

                for(int i=0;i<e.getPointerCount();i++)
                {
                    Log.d("pointer",String.format("i=%s id=%s",i,e.getPointerId(i)));

                    touch1.setCollideable(true);
                    touch1.getPosition().x = e.getX(i)*GameWorldProperties.getWidthRatio();
                    touch1.getPosition().y = e.getY(i)*GameWorldProperties.getHeightRatio();

                    touch2.setCollideable(true);
                    touch2.getPosition().x = e.getX(i)*GameWorldProperties.getWidthRatio();
                    touch2.getPosition().y = e.getY(i)*GameWorldProperties.getHeightRatio();
                }

            break;
        }
        default:
        {
            break;
        }
    }
4

1 に答える 1

1

お役に立てれば。これはBeginning Android Game Programming bookからのものです。

public boolean onTouch(View v, MotionEvent event) {
    synchronized (this) {
        int action = event.getAction() & MotionEvent.ACTION_MASK;
        int pointerIndex = (event.getAction() & MotionEvent.ACTION_POINTER_ID_MASK) >> MotionEvent.ACTION_POINTER_ID_SHIFT;
        int pointerId = event.getPointerId(pointerIndex);
        TouchEvent touchEvent;

        switch (action) {
        case MotionEvent.ACTION_DOWN:
        case MotionEvent.ACTION_POINTER_DOWN:
            touchEvent = touchEventPool.newObject();
            touchEvent.type = TouchEvent.TOUCH_DOWN;
            touchEvent.pointer = pointerId;
            touchEvent.x = touchX[pointerId] = (int) (event
                    .getX(pointerIndex) * scaleX);
            touchEvent.y = touchY[pointerId] = (int) (event
                    .getY(pointerIndex) * scaleY);
            isTouched[pointerId] = true;
            touchEventsBuffer.add(touchEvent);
            break;

        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_POINTER_UP:
        case MotionEvent.ACTION_CANCEL:
            touchEvent = touchEventPool.newObject();
            touchEvent.type = TouchEvent.TOUCH_UP;
            touchEvent.pointer = pointerId;
            touchEvent.x = touchX[pointerId] = (int) (event
                    .getX(pointerIndex) * scaleX);
            touchEvent.y = touchY[pointerId] = (int) (event
                    .getY(pointerIndex) * scaleY);
            isTouched[pointerId] = false;
            touchEventsBuffer.add(touchEvent);
            break;

        case MotionEvent.ACTION_MOVE:
            int pointerCount = event.getPointerCount();
            for (int i = 0; i < pointerCount; i++) {
                pointerIndex = i;
                pointerId = event.getPointerId(pointerIndex);

                touchEvent = touchEventPool.newObject();
                touchEvent.type = TouchEvent.TOUCH_DRAGGED;
                touchEvent.pointer = pointerId;
                touchEvent.x = touchX[pointerId] = (int) (event
                        .getX(pointerIndex) * scaleX);
                touchEvent.y = touchY[pointerId] = (int) (event
                        .getY(pointerIndex) * scaleY);
                touchEventsBuffer.add(touchEvent);
            }
            break;
        }

        return true;
    }
}
于 2013-06-20T19:17:59.453 に答える