我正在做一个简单的游戏,玩家在屏幕上绘制形状。路径的点存储在一个数组中,并在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值都应存储在player
arrayList中。最后,所有player
arrayList应存储在名为players
的主arrayList中。我可以帮忙解决这个问题吗?我不知道如何开始。
我在GameLoop类中声明了我的列表:
// Lists to handle multiple touch input
players = new ArrayList<List<Point>>(); // Main arrayList
player = new ArrayList<Point>(); // Inner arrayList
以下代码位于MainActivity类中,我使用GameLoop
之类的对象(如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;
}
答案 0 :(得分: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));
}
}
您必须为多个指针创建多个数组。您可以使用HashMap<Integer, List<Point>>
从指针ID映射到该指针的Point
列表。
以下是我在上面发布的示例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);
}