user891123
user891123

Reputation: 391

How to handle multitouch

I am writing an app in xcode using box2d. Right now I am using the code below. The problem is that it will only handle one touch event. How can I make my code handle all of the touch events, in this case check the location of each touch. I also want to store the touches so that when they end I can use the proper code to end whatever the individual touches started.

-(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

    }
}

Upvotes: 0

Views: 600

Answers (4)

Singhak
Singhak

Reputation: 8896

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

  }
}

Upvotes: 0

Randall
Randall

Reputation: 14839

In addition to iterating through the set of touches, you'll need to make sure that the view is multi-touch enabled. This can be done in Interface Builder/Xcode 4

Upvotes: 1

mahboudz
mahboudz

Reputation: 39376

You can get the number of fingers touching the screen with:

NSSet *touchEvents = [event allTouches];

You can get each touches individual location, multi-taps, etc., using and enumerated for loop and stepping through touchEvents.

Upvotes: 1

Daniel G. Wilson
Daniel G. Wilson

Reputation: 15055

It should be something like this:

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

Upvotes: 1

Related Questions