3

我正在做一个简单的游戏,玩家在屏幕上绘制形状。路径的点存储在一个数组中并在 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。必须有多个玩家同时绘制形状的可能。所有 Point 值都应存储在playerarrayList 中。最后,所有playerarrayList 都应该存储在一个名为的主 arrayListplayers中。我可以得到一些帮助来解决这个问题吗?我不知道如何开始。

我已经在 GameLoop 类中声明了我的列表:

        // Lists to handle multiple touch input
    players = new ArrayList<List<Point>>(); // Main arrayList
    player = new ArrayList<Point>(); // Inner arrayList

下面的代码在 MainActivity 类中,我使用GameLooplike对象gameLoop与 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; 
}
4

1 回答 1

0

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

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));
    }
}

您将必须为多个指针创建多个数组。您可以使用 aHashMap<Integer, List<Point>>从指针 ID 映射到Point该指针的 s 列表。

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);
}
于 2013-05-11T07:40:32.770 回答