Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/94.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
点击之间的iOS计时_Ios_Objective C_Timing - Fatal编程技术网

点击之间的iOS计时

点击之间的iOS计时,ios,objective-c,timing,Ios,Objective C,Timing,我试着在敲击声之间保持节奏。然而,我随机得到了巨大的价值,我不知道为什么 @implementation GameScene { CFTimeInterval previousFrameTime; SKLabelNode* myLabel; } -(void)didMoveToView:(SKView *)view { previousTimeFrame = 0.0f; myLabel = [SKLabelNode labelNodeWithFontNamed:@"Ch

我试着在敲击声之间保持节奏。然而,我随机得到了巨大的价值,我不知道为什么

@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我觉得这条线好像断了:

previousFrameTime=CACurrentMediaTime()-previousFrameTime

让我们看看如果你每秒钟点击一次,这将如何工作,准确地说:

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

时间增量计算是正确的,但您必须记录最后测量的时间,而不是最后计算的间隔,因此

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;

下面是一个演示如何计算平均点击率的答案-它可能会给您一些帮助