我在做一个简单的游戏,玩家在屏幕上画形状。路径的点存储在数组中,并在类gameloop中绘制。我差不多完成了,但是现在我意识到我应该用多点触控输入来完成游戏,让两个玩家同时绘制图形!
我知道我需要对第一个手指使用event.Action_Down,对下一个手指使用event.Action_Pointer_Down,但如何处理所有形状的点和路径的绘制?我可以只使用一个数组,还是需要每个数组。感觉我需要加倍gameloop类中的所有代码来检查这两条路径?我的问题可能有点不清楚,但我现在的感觉是这样的!有什么建议就好了!
这是我如何处理单触事件并将所有点传递给数组的代码:

@Override
public boolean onTouch(View v, MotionEvent event) {

    synchronized (gameLoop) {

        switch (event.getAction()) {

        case MotionEvent.ACTION_DOWN:
            gameLoop.touchDownX = event.getX();
            gameLoop.touchDownY = event.getY();
            break;

        case MotionEvent.ACTION_MOVE:
            Point point = new Point();
            point.x = (int) event.getX();
            point.y = (int) event.getY();
            gameLoop.addPoints(point);
            gameLoop.startDrawLine = true;
            break;

        case MotionEvent.ACTION_UP:
            Point point2 = new Point();
            point2.x = (int) gameLoop.touchDownX;
            point2.y = (int) gameLoop.touchDownY;
            gameLoop.addPoints(point2); // Add last point to close shape
            gameLoop.pathOK = true;
            gameLoop.touchActionUp = true;
            break;
        }
    }
    return true;
}

编辑:
这很复杂!我发现了一些我修改过的代码。我需要为每个开始在屏幕上绘制形状的人创建一个arraylist。必须有可能让几个玩家同时绘制形状。所有点值都应存储在player数组列表中。最后,所有的arraylist都应该存储在名为player的主arraylist中。我能找人帮忙解决这个问题吗?我不知道怎么开始。
我已经在gameloop类中声明了我的列表:
        // Lists to handle multiple touch input
    players = new ArrayList<List<Point>>(); // Main arrayList
    player = new ArrayList<Point>(); // Inner arrayList

下面的代码在mainActivity类中,我使用类似于players的对象来与gameloop方法通信。
@Override
public boolean onTouch(View v, MotionEvent event) {

    synchronized (gameLoop) {

    for(int i=0; i<event.getPointerCount(); i++) { // Numbers of pointers on screen

        int id = event.getPointerId(i);

        // Check if fingers touch screen
        if (event.getActionIndex() == i && (event.getActionMasked() == MotionEvent.ACTION_POINTER_UP || event.getActionMasked() == MotionEvent.ACTION_UP )) {


        }

        // Check if fingers leave the screen
        else if (event.getActionIndex() == i && (event.getActionMasked() == MotionEvent.ACTION_POINTER_DOWN || event.getActionMasked() == MotionEvent.ACTION_DOWN)) {


        }

        // Check movement on screen
        else {

        }
    }
    }
    return true;
}

最佳答案

看看这个例子,它一次处理所有指针的运动历史:

void printSamples(MotionEvent ev) {
    final int historySize = ev.getHistorySize();
    final int pointerCount = ev.getPointerCount();
    for (int h = 0; h < historySize; h++) {
        System.out.printf("At time %d:", ev.getHistoricalEventTime(h));
        for (int p = 0; p < pointerCount; p++) {
            System.out.printf("  pointer %d: (%f,%f)",
            ev.getPointerId(p), ev.getHistoricalX(p, h), ev.getHistoricalY(p, h));
        }
    }

    System.out.printf("At time %d:", ev.getEventTime());
    for (int p = 0; p < pointerCount; p++) {
        System.out.printf("  pointer %d: (%f,%f)",
        ev.getPointerId(p), ev.getX(p), ev.getY(p));
    }
}

必须为多个指针创建多个数组。您可以使用HashMap<Integer, List<Point>>将指针id映射到该指针的Points列表。
下面是MotionEvent的指南,上面是我发布的示例:
http://developer.android.com/reference/android/view/MotionEvent.html
编辑:尝试将代码与您的需要匹配:
private HashMap<Integer, List<Point>> myPoints;

private void addPoints(MotionEvent ev) {
    final int historySize = ev.getHistorySize();
    final int pointerCount = ev.getPointerCount();
    for (int h = 0; h < historySize; h++) {
        for (int p = 0; p < pointerCount; p++) {
            addPoint(ev.getPointerId(p), new Point(ev.getHistoricalX(p, h), ev.getHistoricalY(p, h)));
        }
    }

    System.out.printf("At time %d:", ev.getEventTime());
    for (int p = 0; p < pointerCount; p++) {
        addPoint(ev.getPointerId(p), new Point(ev.getHistoricalX(p, h), ev.getHistoricalY(p, h)));
    }
}

private void addPoint(Integer pointerId, Point point) {
    List<Point> points = myPointers.get(pointerId);

    if (points == null) {
        points = new LinkedList<Point>();
        myPointers.put(pointerId, points);
    }

    myPointers.get(pointerId).add(point);
}

10-04 23:00