2011-03-31 63 views
0

我試圖跟蹤多點觸控的序列,在這裏我所做的:如何按順序檢測多點觸摸?

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

我首先嚐試了「[觸及allObjects]」,但這需要同時進行多點觸控。我在這裏想要的是 一次接收多點觸摸。所以我用[事件allTouches];

4.I通過一次把在我的手指一個測試的代碼,但 輸出序列似乎raomdom(具有小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?

希望有人能幫助我,謝謝^ _^

回答

1

您可以自己存儲觸摸。在ccTouchesBegan的第一次調用中,將所有觸摸存儲在您的列表中。在後續調用中,將您列表中尚未包含的所有內容附加到列表的末尾。然後,當調用ccTouchesEnded時,擦除列表。