点击之间的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;
下面是一个演示如何计算平均点击率的答案-它可能会给您一些帮助