2013-01-09 82 views
0

我寫了一些代碼行,我可以選擇精靈,我可以拖動它,但只要我拖動它,它會離開鼠標光標一些像素,然後我可以控制它,但它是從我的鼠標cursos一些像素遠..好像跟我CGPoint轉換做錯了事,或者我不知道,這是我的代碼用鼠標和Cocos2d拖動精靈

- (void)selectSpriteForTouch:(CGPoint)touchLocation { 
    CCSprite * newSprite = nil; 
    for (CCSprite *sprite in movableSprites) { 
     if (CGRectContainsPoint(sprite.boundingBox, touchLocation)) { 
      newSprite = sprite; 
      NSLog(@"palieciau"); 
      break; 
     } 
    } 
    if (newSprite != selSprite) { 
     [selSprite stopAllActions]; 
     [selSprite runAction:[CCRotateTo actionWithDuration:0.1 angle:0]]; 
     CCRotateTo * rotLeft = [CCRotateBy actionWithDuration:0.1 angle:-4.0]; 
     CCRotateTo * rotCenter = [CCRotateBy actionWithDuration:0.1 angle:0.0]; 
     CCRotateTo * rotRight = [CCRotateBy actionWithDuration:0.1 angle:4.0]; 
     CCSequence * rotSeq = [CCSequence actions:rotLeft, rotCenter, rotRight, rotCenter, nil]; 
     [newSprite runAction:[CCRepeatForever actionWithAction:rotSeq]]; 
     selSprite = newSprite; 
    } 
} 

- (BOOL)ccMouseDown:(NSEvent*)event { 
    CCSprite * newSprite = nil; 
    CGPoint clickLocation = [[CCDirector sharedDirector] convertEventToGL:event]; 
    for (CCSprite *sprite in movableSprites) { 
     if (CGRectContainsPoint(sprite.boundingBox, clickLocation)) { 
      newSprite = sprite; 
      break; 
     } 
    } 
    if (newSprite != selSprite) { 
     [selSprite stopAllActions]; 
     [selSprite runAction:[CCRotateTo actionWithDuration:0.1 angle:0]]; 
     CCRotateTo * rotLeft = [CCRotateBy actionWithDuration:0.1 angle:-4.0]; 
     CCRotateTo * rotCenter = [CCRotateBy actionWithDuration:0.1 angle:0.0]; 
     CCRotateTo * rotRight = [CCRotateBy actionWithDuration:0.1 angle:4.0]; 
     CCSequence * rotSeq = [CCSequence actions:rotLeft, rotCenter, rotRight, rotCenter, nil]; 
     [newSprite runAction:[CCRepeatForever actionWithAction:rotSeq]]; 
     selSprite = newSprite; 
    } 
} 


- (void)panForTranslation:(CGPoint)translation { 
    if (selSprite) { 
     CGPoint newPos = ccpAdd(selSprite.position, translation); 
     selSprite.position = newPos; 
    } else { 
    } 
} 

-(BOOL)ccMouseDragged:(NSEvent *)event { 
    CGPoint point = [[CCDirector sharedDirector] convertEventToGL:event]; 
    CGPoint mouseLocation = [self convertToNodeSpace:point]; 
    CGPoint translation = ccpSub(point, oldMouseLocation_); 
    [self panForTranslation:translation]; 
    oldMouseLocation_ = point; 
} 
+0

當'ccMouseDown:'第一次調用'ccMouseDragged:'時,'oldMouseLocation_'的值是什麼? – Kreiri

回答

0

有些事情你應該考慮:

  1. 在開始拖動時,您應該將oldMouseLocation設置爲啓動拖動的點
  2. 確保你的精靈被從他們的中心拖走。例如,如果你正在移動的容器,你可以從他們的左下角
  3. 在你ccMouseDown您不使用convertToNodeSpace功能
  4. 拖動你可能會考慮在移動鼠標拖動的精靈(selSprite)位置,而不是使用[oldMouseLocation - mouseLocation]距離進行翻譯。可選地,你可以應用一些在[開始拖動點 - 拖動精靈位置]上計算的偏移距離
0

我明白了,謝謝。

- (BOOL)ccMouseDragged:(NSEvent *)event 
{ 
    CGPoint point = [[CCDirector sharedDirector] convertEventToGL:event]; 
    CGPoint mouseLocation = [self convertToNodeSpace:point]; 
    CGPoint translation = (mouseLocation); 
    [self panForTranslation:translation]; 
    return YES; 
} 

-(void)panForTranslation:(CGPoint)translation 
{ 
    if (first) 
    { 
     NSLog(@"%f, %f",translation.x, translation.y); 
     deb1.position = ccp(translation.x, translation.y); 
    } 
    if (second) 
    { 
     NSLog(@"%f, %f",translation.x, translation.y); 
     deb2.position = ccp(translation.x, translation.y); 
    } 
    if (third) 
    { 
     NSLog(@"%f, %f",translation.x, translation.y); 
     deb3.position = ccp(translation.x, translation.y); 
    } 
    else 
    { 
     NSLog(@"not in sprite's rect"); 
    } 

}