我正试图在水龙头之间保持节奏。但是,我随机获得了巨大的价值,我不知道为什么。
@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 :(得分:5)
这条线似乎对我不了解:
previousFrameTime = CACurrentMediaTime() - previousFrameTime;
让我们来看看如果你精确地点击每一秒,这将是如何工作的:
1.) previousFrameTime = 1000 - 0; (1000)
2.) previousFrameTime = 1001 - 1000; (1)
3.) previousFrameTime = 1002 - 1; (1001)
4.) previousFrameTime = 1003 - 1001; (2)
答案 1 :(得分: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;