views:

227

answers:

1

I have two sprites in my app. Both should have touches enabled and both touches are independent of one another. And if I touch the screen (not on sprites) it should have different touches. My problem is all three sprite1, sprite2, remaining screen should have independent touches. But my program is taking all the touches as same. How can I make them as what I needed ?

Thank You.

+1  A: 

Hello, to do this, first you need to enable multi touch for your application:

    [self setMultipleTouchEnabled:YES];

Then to identify touches, you can use something like the following code:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
   for ( UITouch* Touch in touches ) 
   { 
      printf( "Touch began %p, tapcount %d\n", (void *) Touch, [Touch tapCount] ); 
      fflush( stdout ); 
   } 
} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{  
   for ( UITouch* Touch in touches ) 
   { 
      printf( "Touch moved %p, tapcount %d\n", (void*)Touch, [Touch tapCount] ); 
      fflush( stdout ); 
   } 
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
   for ( UITouch* Touch in touches ) 
   { 
      printf( "Touch ended %p, tapcount %d\n", (void*)Touch, [Touch tapCount] ); 
      fflush( stdout ); 
   } 
}

So with the (void*)Touch, you can identify a particular touch pointer, which will not change until you actually "end" that touch.

For example, if you touch the screen, you will get a touch instance that will remain with the same memory address even if you move that finger, until you release it. Good luck with this, I used the bases of this code exactly for multi touch sprite management.

Mr.Gando