2011-09-21 109 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); 

    } 
}