2014-02-12 14 views
0

시침이 분침 뒤에 오는 아날로그 시계를 만들려고 노력 중입니다. 내가 생각할 수있는 모든 시도가 있지만 비율은 꺼져 있습니다.SpriteKit - zRotion 비율이

분침을 끌면 올바르게 작동하므로 시침이 회전해야하는 비율을 계산하는 방법이라고 생각합니다.

이 많은 내 가장 최근의 시도는 ...

코드 :

#define DEGREES_TO_RADIANS(angle) ((angle)/180.0 * M_PI) 
#define RADIANS_TO_DEGREES(radians) ((radians) * (180.0/M_PI)) 

CGFloat oldAngleInRadians = M_PI_2; 

- (void)rotateNode:(SKSpriteNode *)node forTouch:(UITouch *)touch 
{ 
    if ([node isEqual:dial]) return; 

     CGPoint positionInDial = [touch locationInNode:dial]; 
     float deltaY = positionInDial.y - minuteIndicator.anchorPoint.y; 
     float deltaX = positionInDial.x - minuteIndicator.anchorPoint.x; 
     CGFloat angleInRadians = atan2f(deltaY, deltaX); 

     // Minute hand rotation in this case 
     [node runAction:[SKAction rotateToAngle:angleInRadians - (M_PI/2) duration:0]]; 

    if (self.difficulty == kMediumDifficulty) { 
     [self updateHourhandAngleWithNewAngle:angleInRadians oldAngle:oldAngleInRadians]; 

     oldAngleInRadians = angleInRadians; 
    } 
} 

#pragma mark - Rotate hour hand with minute hand 

- (void)updateHourhandAngleWithNewAngle:(CGFloat)newAngle oldAngle:(CGFloat)oldAngle 
{ 
    double newAngleDeg = RADIANS_TO_DEGREES(newAngle); 
    double oldAngleDeg = RADIANS_TO_DEGREES(oldAngle); 

    double differenceDeg = 0; 

    if (newAngleDeg > 0 && oldAngleDeg < 0) { 
     differenceDeg = oldAngleDeg - (- 1 * newAngleDeg); 
    } else if (newAngleDeg < 0 && oldAngleDeg > 0) { 
     differenceDeg = fabsf(newAngleDeg) - oldAngleDeg; 
    } else { 
     differenceDeg = newAngleDeg - oldAngleDeg; 
    } 

    [hourIndicator runAction:[SKAction rotateByAngle:DEGREES_TO_RADIANS((differenceDeg/12.0f)) duration:0]]; 
} 

답변

1
내가 제대로 문제를 이해 확실하지 경우

, 그러나 이것은 내가 SpriteKit를 사용하여 아날로그 시계를 만들 것입니다 방법입니다 :

// MyScene.m 
- (void) didMoveToView:(SKView *)view 
{ 
    [self.clock didEnterScene]; 
} 

- (void) update:(CFTimeInterval)currentTime 
{ 
    CGFloat dt = 0.0; 
    if (self.lastUpdateInterval > 0) 
    { 
     dt = currentTime - self.lastUpdateInterval; 
    } 
    [self.clock update:dt]; 
    self.lastUpdateInterval = currentTime; 
} 

// ClockNode.m 
- (void) didEnterScene 
{ 
    SKSpriteNode* secHand = [SKSpriteNode spriteNodeWithColor:[UIColor blueColor] size:CGSizeMake(2, 100)]; 
    SKSpriteNode* minHand = [SKSpriteNode spriteNodeWithColor:[UIColor greenColor] size:CGSizeMake(5, 100)]; 
    SKSpriteNode* hourHand = [SKSpriteNode spriteNodeWithColor:[UIColor redColor] size:CGSizeMake(10, 70)]; 

    secHand.position = CGPointMake(CGRectGetMidX(self.scene.frame), CGRectGetMidY(self.scene.frame)); 
    minHand.position = secHand.position; 
    hourHand.position = secHand.position; 

    secHand.anchorPoint = CGPointMake(0.5, 1.0); 
    minHand.anchorPoint = CGPointMake(0.5, 1.0); 
    hourHand.anchorPoint = CGPointMake(0.5, 1.0); 

    [self addChild:secHand]; 
    [self addChild:minHand]; 
    [self addChild:hourHand]; 

    _secHand = secHand; 
    _minHand = minHand; 
    _hourHand = hourHand; 

    _secHand.zRotation = M_PI; 
    _minHand.zRotation = M_PI; 
    _hourHand.zRotation = M_PI; 

    _msec = 0.0f; 
    _sec = 0.0f; 
    _min = 0.0f; 
    _hour = 0.0f; 
} 

- (void) update:(CGFloat)dt 
{ 
    _msec += dt; 
    if (_msec >= 1) 
    { 
     _msec -= 1; 
     _sec += 1; 
     _secHand.zRotation -= 2*M_PI/60; 
    } 
    if (_sec >= 60) 
    { 
     _sec -= 60; 
     _min += 1; 
     _minHand.zRotation -= 2*M_PI/60; 
    } 
    if (_min >= 60) 
    { 
     _min -= 60; 
     _hour += 1; 
     _hourHand.zRotation -= 2*M_PI/12; 
    } 
    if (_hour >= 12) 
    { 
     _hour -= 12; 
    } 
} 

Analog clock