I want to simultaneously control two sprites in my game. I have enabled MultiTouch and I have the following code below. When the first finger goes on Object1, it moves and responds as expected. However, when the second finger goes on the screen (anywhere on the screen), object 1 tries to go to that location instead. It's preventing me from moving the object2. I know I am doing something wrong with the touches. I think what is happening is that the First Touch is being detected so the first IF statement is always getting executed, hence trying to move the first object to the new location.
Any pointers appreciated.
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if (_mouseJoint != NULL) return;
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);
if (_Object1->TestPoint(locationWorld)) {
b2MouseJointDef md;
md.bodyA = _groundBody;
md.bodyB = _objectBody1;
md.target = locationWorld;
md.collideConnected = true;
md.maxForce = 1000.0f * Object1->GetMass();
_mouseJoint = (b2MouseJoint *)_world->CreateJoint(&md);
_Object1->SetAwake(true);
}
// DETECT SECOND OBJECT Here
if (_Object2->TestPoint(locationWorld)) {
b2MouseJointDef md;
md.bodyA = _groundBody;
md.bodyB = _objectBody2;
md.target = locationWorld;
md.collideConnected = true;
md.maxForce = 1000.0f * Object1->GetMass();
_mouseJoint = (b2MouseJoint *)_world->CreateJoint(&md);
_Object2->SetAwake(true);
}
}