如何依次检测多点触摸?
我尝试按顺序跟踪多点触摸,我做了什么:
1.创建一个 cocos2d 应用程序 2.启用多点触控 3.在touchesBegin中添加代码:
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSSet *allTouches = [event allTouches];
for (int i = 0; i < allTouches.count; i++) {
UITouch *touch = [[allTouches allObjects] objectAtIndex:i];
CGPoint touchLocation = [touch locationInView: [touch view]];
CGPoint location = [[CCDirector sharedDirector]
convertToGL:touchLocation];
NSLog(@"allTouches %d %0.1f, %0.1f", (i + 1), location.x, location.y);
}
}
我首先尝试了“[touches allObjects]”,但这需要 多点触摸同时发生。我在这里想要的是 一次接收多点触摸。所以我使用了 [event allTouches];
4.我通过一次放一根手指来测试代码,但是 输出序列似乎是随机的(第一个手指具有较小的xVal,第二个手指具有较小的xVal) xVal 大时,间隔大于 2s):
result 1:
2011-03-31 10:54:35.847 MultiTouchDemo[2788:207] allTouches 1 58.0, 458.0
2011-03-31 10:54:38.045 MultiTouchDemo[2788:207] allTouches 1 56.0, 453.0
2011-03-31 10:54:38.046 MultiTouchDemo[2788:207] allTouches 2 930.0, 429.0
result 2:
2011-03-31 10:55:11.659 MultiTouchDemo[2788:207] allTouches 1 86.0, 415.0
2011-03-31 10:55:13.378 MultiTouchDemo[2788:207] allTouches 1 965.0, 409.0
2011-03-31 10:55:13.380 MultiTouchDemo[2788:207] allTouches 2 84.0, 413.0
result 3:
2011-03-31 10:55:32.991 MultiTouchDemo[2788:207] allTouches 1 76.0, 453.0
2011-03-31 10:55:34.630 MultiTouchDemo[2788:207] allTouches 1 877.0, 430.0
2011-03-31 10:55:34.631 MultiTouchDemo[2788:207] allTouches 2 76.0, 455.0
result 4:
2011-03-31 10:55:45.960 MultiTouchDemo[2788:207] allTouches 1 94.0, 440.0
2011-03-31 10:55:47.134 MultiTouchDemo[2788:207] allTouches 1 92.0, 438.0
2011-03-31 10:55:47.136 MultiTouchDemo[2788:207] allTouches 2 934.0, 358.0
所以你可以在 [allTouches allObjects] 中看到第二根手指的索引 似乎一直在改变。
如果我想让游戏支持在一台设备上进行多人游戏, 我想追踪他们执行的动作,我该如何做到这一点 所有这些随机的 pos?
希望有人能帮助我,谢谢^_^
I tried to trace multi-touch in sequence, here what I did:
1.Create a cocos2d app
2.Enable multi-touch
3.Add code in touchesBegin:
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSSet *allTouches = [event allTouches];
for (int i = 0; i < allTouches.count; i++) {
UITouch *touch = [[allTouches allObjects] objectAtIndex:i];
CGPoint touchLocation = [touch locationInView: [touch view]];
CGPoint location = [[CCDirector sharedDirector]
convertToGL:touchLocation];
NSLog(@"allTouches %d %0.1f, %0.1f", (i + 1), location.x, location.y);
}
}
I tried the "[touches allObjects]" at first, but that requires the
multi-touch to take place at the same time. What I want here is to
receive multi-touch on at a time. So I used [event allTouches];
4.I tested the code by putting on my finger one at a time, but the
output sequence seems raomdom (first finger with small xVal and second
with big xVal, interval is bigger than 2s):
result 1:
2011-03-31 10:54:35.847 MultiTouchDemo[2788:207] allTouches 1 58.0, 458.0
2011-03-31 10:54:38.045 MultiTouchDemo[2788:207] allTouches 1 56.0, 453.0
2011-03-31 10:54:38.046 MultiTouchDemo[2788:207] allTouches 2 930.0, 429.0
result 2:
2011-03-31 10:55:11.659 MultiTouchDemo[2788:207] allTouches 1 86.0, 415.0
2011-03-31 10:55:13.378 MultiTouchDemo[2788:207] allTouches 1 965.0, 409.0
2011-03-31 10:55:13.380 MultiTouchDemo[2788:207] allTouches 2 84.0, 413.0
result 3:
2011-03-31 10:55:32.991 MultiTouchDemo[2788:207] allTouches 1 76.0, 453.0
2011-03-31 10:55:34.630 MultiTouchDemo[2788:207] allTouches 1 877.0, 430.0
2011-03-31 10:55:34.631 MultiTouchDemo[2788:207] allTouches 2 76.0, 455.0
result 4:
2011-03-31 10:55:45.960 MultiTouchDemo[2788:207] allTouches 1 94.0, 440.0
2011-03-31 10:55:47.134 MultiTouchDemo[2788:207] allTouches 1 92.0, 438.0
2011-03-31 10:55:47.136 MultiTouchDemo[2788:207] allTouches 2 934.0, 358.0
So you can see the index of the 2nd finger in the [allTouches allObjects]
seems to change all the time.
If I want to make a game support multi-player on a single device,
and I want to trace the move they perform, how could I do this with
all this random pos?
Hope someone can help me, thanks^_^
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以自己存储触摸。第一次调用 ccTouchesBegan 时,将所有触摸存储在列表中。在后续调用中,将列表中尚未存在的任何触摸附加到列表末尾。然后,当调用 ccTouchesEnded 时,擦除列表。
You could store the touches yourself. On the first call of ccTouchesBegan, store all touches in your list. On subsequent calls, append any touches not already in your list to the end of your list. Then when ccTouchesEnded is called, wipe the list.