2011-09-21 75 views
0

我正在使用box2d在xcode中編寫應用程序。現在我正在使用下面的代碼。問題是它只能處理一次觸摸事件。我怎樣才能讓我的代碼處理所有的觸摸事件,在這種情況下,檢查每個觸摸的位置。我也想存儲觸摸,以便當它們結束時,我可以使用正確的代碼來結束任何單個觸摸開始。如何處理多點觸控

-(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 

    UITouch *myTouch = [touches anyObject]; 
    CGPoint location = [myTouch locationInView:[myTouch view]]; 
    location = [[CCDirector sharedDirector] convertToGL:location]; 
    b2Vec2 locationWorld = b2Vec2(location.x/PTM_RATIO, location.y/PTM_RATIO); 
    CGSize screenSize = [CCDirector sharedDirector].winSize; 

    if (locationWorld.x >= screenSize.width*2/5/PTM_RATIO && locationWorld.x <= screenSize.width*3.25/5/PTM_RATIO) { 
     //do something 
    } 
    else if (0 && locationWorld.x <= screenSize.width*2/5/PTM_RATIO) { 
     //do something else 

    } 
} 

回答

1

應該是這樣的:

- (void)ccTouchesBegan:(NSSet*)touches withEvent:(UIEvent*)event 
{ 
    for (UITouch *touch in touches) 
    { 
     if (touch.phase == UITouchPhaseBegan) 
     { 
      // Insert code here 
     } 
    } 
} 
1

你可以手指觸摸屏幕的數量:你可以得到每個單獨的觸摸位置

NSSet *touchEvents = [event allTouches]; 

,多水龍頭等,使用和枚舉循環和逐步touchEvents。

1

除了遍歷這組觸摸,您還需要確保該視圖已啓用多點觸控。這可以在界面生成器來完成/ Xcode的4

0

在cocos2d-X

void LayerHero::ccTouchesEnded(CCSet* touches, CCEvent* event) 
{ 
CCTouch* touch = (CCTouch*)(touches->anyObject()); 
CCPoint location = touch->getLocationInView(); 
location = CCDirector::sharedDirector()->convertToGL(location); 
CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize(); 

if(location.x<visibleSize.width/2) 
{ 

} 
else if(location.x>visibleSize.width/2) 
{ 
    CCLOG("We are in the touch2 %f",location.x); 

    } 
}