Handling CCSprites with Multiple Touch

50 Views Asked by At

I have one or more than one sprites stored in a array. What i want is to drag these sprites with multiple fingers around the screen. i.e (Multiple Touch) Can anyone tell me how to do that?

Any help will be appreciated..

Here is my code:

-(void)touchBegan:(CCTouch *)touch withEvent:(CCTouchEvent *)event
{

  CGPoint location = [touch locationInNode:self];

  selectedSprite.paused = YES;

  [self selectSpriteFromTouch:location]; 
   //this is where i get sprite selected from i.e selectedSprite from array


 }
 -(void)touchMoved:(CCTouch *)touch withEvent:(CCTouchEvent *)event
 {

   selectedSprite.position = [touch locationInNode:self];


  }

 -(void)touchEnded:(CCTouch *)touch withEvent:(CCTouchEvent *)event
 {

  selectedSprite.paused = NO;
  selectedSprite.position = [touch locationInNode:self];

   selectedSprite = nil;



   }


  -(void)touchCancelled:(CCTouch *)touch withEvent:(CCTouchEvent *)event
  {

    selectedSprite.paused = NO;
    selectedSprite = nil;


    }
1

There are 1 best solutions below

0
virgil debique On

If I understand correctly you want say two fingers on the screen, each to move 1 sprite? I guess you have the images in an array so get the location of each touch and if each touch is on a sprite, then you know which touch is on which sprite.

CGPoint fingerOne = [event.allTouches.allValues[0] locationInWorld];
CGPoint fingerTwo = [event.allTouches.allValues[1] locationInWorld];

If sprite at index 0 is touched by fingerOne, then you can move that by the same amount as fingerOne moves and similarly for fingerTwo and sprite at index 1.