2014-12-30 10 views
2

나는 탭 사이의 리듬을 유지하려고합니다. 그러나, 나는 무작위로 거대한 가치를 얻고 나는 왜 확실하지 않다.iOS 사이의 타이밍

@implementation GameScene 
{ 
    CFTimeInterval previousFrameTime; 
    SKLabelNode* myLabel; 
} 

-(void)didMoveToView:(SKView *)view { 
    previousTimeFrame = 0.0f; 
    myLabel = [SKLabelNode labelNodeWithFontNamed:@"Chalkduster"]; 

    myLabel.text = @" "; 
    myLabel.fontSize = 12; 
    myLabel.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame)); 
[self addChild:myLabel]; 
} 

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    myLabel.text = [NSSTring stringWithFormat: @"%f", previousFrameTime]; 
} 

//Called every frame 
-(void)update:(CFTimeInterval)currentTime { 
    //get the time between frames 
    previousFrameTime = CACurrentMediaTime() - previousFrameTime; 
} 

출력 : 0.65323 0.93527 1.65326 5866.42930 < - ???? 2.52442 5.23156 5888.21345 < - ?????

이러한 임의 점프의 원인은 무엇입니까?

+0

다음은 평균 탭 속도를 계산하는 방법을 보여주는 답변입니다 - 도움이 될 수 있습니다 http://stackoverflow.com/questions/26666972/how-can-i-calculate-the-instantaneous- or-close-to-taps- 초당 쿠키 - 26667164 # 26667164 – Paulw11

답변

5

이 줄은 나에게 깨진 것 같다 :

previousFrameTime = CACurrentMediaTime() - previousFrameTime;

당신이 매 초마다 정확하게 누른 경우의 이것이 어떻게 작동하는지 살펴 보자 :

1.) previousFrameTime = 1000 - 0; (1000) 
2.) previousFrameTime = 1001 - 1000; (1) 
3.) previousFrameTime = 1002 - 1; (1001) 
4.) previousFrameTime = 1003 - 1001; (2) 
+0

이 답변을 좋아합니다. 물고기를 먹는 법을 가르쳐주세요. –

1

시간 델타 계산이 정확하지만, 마지막으로 계산 된 간격이 아니라 마지막 측정 된 시간을 기록해야합니다 ...

CFTimeInterval currentMediaTime = CACurrentMediaTime(); 
CFTimeInterval currentInterval = currentMediaTime - previousFrameTime; 

// use currentInterval however you were using previousFrameTime, but now the 
// previous time should be recorded as the current time 

previousFrameTime = currentMediaTime;