2010-10-14 5 views
2

Comment faire pour faire pivoter l'image-objet autour de son centre dans cocos2d dans ccTouchesMoved. J'ai trouvé du code, mais ce n'est pas ce dont j'ai besoin. J'ai besoin de faire pivoter le sprite comme une photo dans l'application Galerie de photos.Comment faire pour faire pivoter et redimensionner une image-objet comme une image dans la bibliothèque lybrary


- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    NSArray *allTouch = [touches allObjects]; 
    if([allTouch count] > 1){ 
     UITouch *touch = [allTouch objectAtIndex:0]; 
     CGPoint point = [touch locationInView: [touch view]]; 
     point = [[CCDirector sharedDirector] convertToGL:point]; 

     float angleRadians = atanf((float)point.y/(float)point.x); 
     float angleDegrees = CC_RADIANS_TO_DEGREES(angleRadians); 
     float cocosAngle = -1 * angleDegrees; 
     imageFromPicker.rotation = imageFromPicker.rotation + cocosAngle; 

    } 
} 

Répondre

1

est probablement pas la meilleure façon, mais im en utilisant ceci:

Quand je touche l'image-objet avec deux doigts, j'utiliser le arctan des deux points pour faire pivoter l'image-objet:

premier i chercher la direction avec cette fonction:

static inline int ccw(CGPoint p0, CGPoint p1, CGPoint p2) 
{ 
    int dx1, dx2, dy1, dy2; 

    dx1 = p1.x - p0.x; dy1 = p1.y - p0.y; 
    dx2 = p2.x - p0.x; dy2 = p2.y - p0.y; 

    int v1 = dx1 * dy2; 
    int v2 = dy1 * dx2; 

    if (v1 > v2) 
     return 1; 

    if (v1 < v2) 
     return -1; 

    if ((dx1*dx2 < 0) || (dy1*dy2 < 0)) 
     return -1; 

    if ((dx1*dx1 + dy1*dy1) < (dx2*dx2 + dy2*dy2)) 
     return 1; 

    return 0; 
} 

et après la méthode ccTouchesMoved je fais ceci:

if ([allTouches count] == 2) { 
      rotating=TRUE; 
      NSArray *twoTouches = [allTouches allObjects]; 
      UITouch *first = [twoTouches objectAtIndex:0]; 
      UITouch *second = [twoTouches objectAtIndex:1]; 

      CGPoint a = [first previousLocationInView:[touch view]]; 
      CGPoint b = [second previousLocationInView:[touch view]]; 
      CGPoint c = [first locationInView:[touch view]]; 



      int direction =ccw(b, a, c); 
      float pX= fabs(c.x-b.x); 
      float pY= fabs(c.y-b.y); 

      float rotation = atan2(pY, pX); 
     // rotTotal= (rotTotal+rotacion)/10; 
      if(direction<0) 
      YourSprite.rotation=(YourSprite.rotation-rotation); 
      else 
      YourSprite.rotation=(YourSprite.rotation+rotation); 

      } 

ça marche pour moi, espérons que cela fonctionne pour vous aussi

+0

si vous souhaitez faire pivoter plus facilement, vous pouvez ajouter un coeficient flotter ici rotation \t = atan2 (PY, Px) * coef; – JonLOo

+0

Cela fonctionne :) J'ai ajouté un coefficient et son aspect est très bon. J'ai également ajouté la méthode d'écaillage et il reste à ajouter le masque, puis enregistrer l'image-objet. Je vous remercie! – BUDDAx2

+0

vous êtes les bienvenus :) – JonLOo

Questions connexes