2012-07-30 100 views
0

我需要在具有兩個或更多精靈的cocos2d項目中實現一個簡單的捏手勢。你會如此友善地告訴我,我該如何處理俯仰手勢?在cocos2d捏手勢,如何?

我需要的是這樣的:

-(void)handlePinchGesture:(UIPinchGestureRecognizer *)recognizer 
{ 
    if (recognizer.state != UIGestureRecognizerStateCancelled) 
    { 
     if (recognizer.numberOfTouches == 3) 
     { 
      CGPoint firstPoint = [recognizer locationOfTouch:0 inView:recognizer.view]; 
      CGPoint secondPoint = [recognizer locationOfTouch:1 inView:recognizer.view]; 
      CGPoint thirdPoint = [recognizer locationOfTouch:2 inView:recognizer.view]; 
      CGPoint fourthPoint = [recognizer locationOfTouch:3 inView:recognizer.view]; 


      ball1.position=firstPoint; 
      ball2.position=secondPoint; 
      ball3.position=thirdPoint; 
      ball4.position=fourthPoint; 
     } 
    } 
} 

回答

3

這裏我做了什麼(我花了相當多的谷歌搜索)

在實現文件

-(id)init 
{ 
    self = [super init]; 
    self.isTouchEnabled=YES; 


    if (self != nil) 
    {   

    } 
    return self; 
} 
//pinch recognising 
-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    NSSet *allUserTouches=[event allTouches]; 

    if(allUserTouches.count==2) 
    { 
     UITouch* touch1=[[allUserTouches allObjects] objectAtIndex:0]; 
     UITouch* touch2=[[allUserTouches allObjects] objectAtIndex:1]; 

     CGPoint touch1location=[touch1 locationInView:[touch1 view]]; 
     CGPoint touch2location=[touch2 locationInView:[touch2 view]]; 

     touch1location=[[CCDirector sharedDirector] convertToGL:touch1location]; 
     touch2location=[[CCDirector sharedDirector] convertToGL:touch2location]; 

     ball.position=touch1location; 
     newBall.position=touch2location; 

     float currentdist=ccpDistance(touch1location, touch2location); 
     oldDist=currentdist; 
    } 
} 

-(void)ccTouchesMoved:(UITouch *)touch withEvent:(UIEvent *)event 
{ 
    NSSet *allUserTouches=[event allTouches]; 

    if(allUserTouches.count==2) 
    { 
     UITouch* touch1=[[allUserTouches allObjects] objectAtIndex:0]; 
     UITouch* touch2=[[allUserTouches allObjects] objectAtIndex:1]; 

     CGPoint touch1location=[touch1 locationInView:[touch1 view]]; 
     CGPoint touch2location=[touch2 locationInView:[touch2 view]]; 

     touch1location=[[CCDirector sharedDirector] convertToGL:touch1location]; 
     touch2location=[[CCDirector sharedDirector] convertToGL:touch2location]; 

     float currentdist=ccpDistance(touch1location, touch2location); 

     if (oldDist>=currentdist) 
     { 
      //[spriteToZoom setScale:spriteToZoom.scale-fabs((oldDist-currentdist)/100)]; 
      [ball setPosition:touch1location]; 
      [newBall setPosition:touch2location]; 
      NSLog(@"pinch out"); 
     } 
     else 
     { 
      //[spriteToZoom setScale:spriteToZoom.scale+fabs((oldDist-currentdist)/100)]; 
      [ball setPosition:touch1location]; 
      [newBall setPosition:touch2location]; 

      NSLog(@"pinch in"); 
     } 
    } 
}