2013-01-09 1 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

'ccMouseDragged :'가'ccMouseDown :'다음에 처음으로 호출 될 때'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"); 
    } 

}