2012-10-10 2 views
0

나는 a game engine에서 일하고 있는데, 나는 오히려 당황 스럽다. 나는 그것이 단순하다는 것을 확신하지만 진행하기 전에 그것을 알아 내고 싶습니다.내 UIView 애니메이션이 다르게 작동하는 이유는 무엇입니까?

서브 클래스가 UIImageView이고 여러 애니메이션 스프라이트에 대한 지원이 추가되었습니다. 컨트롤도 작동하지만 이상한 행동을보고 있습니다. 내 캐릭터가 위로 걷거나 떠날 때 애니메이션은 프레임을 건너 뛰고 더 빠르게 움직이는 것처럼 보입니다. 오른쪽 또는 아래로 이동하면 속도가 느려지고 올바른 세 프레임이 표시됩니다.

전체 콜 스택을 거쳤습니다. 알아낼 수 없습니다. 결과는 다음과 같습니다.

  • 사용자가 가상 ​​조이패드를 탭합니다.
  • 게임 컨트롤러보기 컨트롤러가 알림을 발송합니다.
  • 알림시 주 플레이어 스프라이트가 나타납니다.
  • 스프라이트가 대리자에게 원하는 곳으로 이동할 수 있는지 묻습니다.
  • 실제로 허용되는 경우 스프라이트는 지정된 거리로 이동합니다.

스프라이트 이동 코드는 다음과 같다 : (NSUInteger의 형식 정의 임) MBSpriteMovement 방향이 위 또는두면

// Move in a direction 
- (void)moveInDirection:(MBSpriteMovementDirection)direction distanceInTiles:(NSInteger)distanceInTiles withCompletion:(void (^)())completion{ 

CGRect oldFrame = [self frame]; 
CGSize tileDimensions = CGSizeZero; 

tileDimensions = [[self movementDataSource] tileSizeInPoints]; 

if (tileDimensions.height == 0 && tileDimensions.width == 0) { 

    NSLog(@"The tile dimensions for the movement method are zero. Did you forget to set a data source?\nI can't do anything with this steaming pile of variables. I'm outta here!"); 

    return; 
} 

CGPoint tileCoordinates = CGPointMake(oldFrame.origin.x/tileDimensions.width, oldFrame.origin.y/tileDimensions.height); 

// Calculate the new position 
if (direction == MBSpriteMovementDirectionLeft || direction == MBSpriteMovementDirectionRight) { 
    oldFrame.origin.x += (tileDimensions.width * distanceInTiles); 
    tileCoordinates.x += distanceInTiles; 
}else{ 
    oldFrame.origin.y += (tileDimensions.height * distanceInTiles); 
    tileCoordinates.y += distanceInTiles; 
} 

if (![[self movementDelegate] sprite:self canMoveToCoordinates:tileCoordinates]) { 
    [self resetMovementState]; 
    if(completion){ 
     completion(); 
    } 
    return; 
} 

[self startAnimating]; 

[UIView animateWithDuration:distanceInTiles*[self movementTimeScaleFactor] delay:0 options:UIViewAnimationOptionCurveLinear | UIViewAnimationOptionBeginFromCurrentState animations:^{ 

    [self setFrame:oldFrame]; 
} 
       completion:^(BOOL finished) { 
        // Perform whatever the callback warrants 
        if(completion){ 
         completion(); 
        } 
        [self resetMovementState]; 
       }]; 
} 

, distanceInTiles 음의 정수이다. 올바른 거리가 계산되지만, 어떤 이유로 든 아래와 오른쪽이 느리게 보입니다. 나는 그것이 위로/왼쪽으로 움직일 때 프레임을 건너 뛰는 것이 확실하다.

왜 그런가?

(이것은 오픈 소스 프로젝트이며, here, on GitHub을 찾을 수 있습니다.)

+0

운동 코드가 표시되지 않습니다. – Mundi

+0

사실 나는 그렇지 않습니다. '[UIView animateWithDuration ...]은 그 코드입니다. startMoving은 실제로'UITableView beginUpdates'와 유사합니다. – Moshe

+0

죄송합니다. 화면에서 벗어났습니다 ... – Mundi

답변

1

당신은 UIView 애니메이션 루틴에 주어진 기간은 음이 아닌 것을 특정 할 필요가있다. 이것은 fabs 함수를 통해 쉽게 수행 할 수 있습니다.

NSTimeInterval animationDuration = fabs(distanceInTiles*[self movementTimeScaleFactor]); 

[UIView animateWithDuration:animationDuration 
         delay:0 
        options:UIViewAnimationOptionCurveLinear | UIViewAnimationOptionBeginFromCurrentState 
       animations:^{ 
        //... 
       }]; 
+0

약간의 테스트를 마친 후에 문제가 생겼습니다! 실제로 음수 값이기 때문에 distanceInTiles를'abs()'메서드에 넣는 것을 선호하지만, 당신의 방식대로하는 것이 더 안전하다고 생각합니다. – Moshe

관련 문제