我有一个CADisplayLink
符合Chipmunk物理,我得到非常缓慢的表现。我在CADisplayLink
更新中调用了方法NSLog
,它被称为平均每秒22次。我的印象是这应该更接近60.我有frameInterval
设置为1,所以应该是60fps,在一个完美的世界?德尔塔时间平均约为0.0167秒(1/60是0.0167,这让我更加困惑)。应该调用CADisplayLink的displayLink每秒多少次?
我的屏幕边界四周只有8个圆形的屏幕,每次打电话时更新到UIButton
个实例,所以我不认为我正在做任何事情,这个程度在我的4S和iPad3上都是如此。我正在用一个单独的方法每2.5秒对每个按钮施加一次随机的力。在模拟器中运行是平滑的,所以这是一个设备唯一的问题。任何人都可以帮助我找出造成经济放缓的原因,以及我能做些什么?
下面是相关的代码,第一个是它建立链路:
[NSTimer scheduledTimerWithTimeInterval: 2.5f target: self selector: @selector(updateForces) userInfo: nil repeats: YES];
_displayLink = [CADisplayLink displayLinkWithTarget: self selector: @selector(update)];
_displayLink.frameInterval = 1;
[_displayLink addToRunLoop: [NSRunLoop mainRunLoop] forMode: NSRunLoopCommonModes];
这里是一个应该被称为(!我想)每秒60次的方法,但只有22个左右的被称为:
if (!gameIsPaused) {
cpFloat dt = _displayLink.duration * _displayLink.frameInterval;
cpSpaceStep([[AGChipmunkSpace sharedInstance] space], dt);
for (LCBall *i in balls) {
cpVect pos1 = cpBodyGetPos(i.body);
CGAffineTransform trans1 = CGAffineTransformMakeTranslation(pos1.x, pos1.y);
CGAffineTransform rot1 = CGAffineTransformMakeRotation(cpBodyGetAngle(i.body));
i.button.transform = CGAffineTransformConcat(rot1, trans1);
}
}
最后,这里有一个叫做每2.5秒方法,应用随机力(updateForces):
if (!gameIsPaused) {
for (LCBall *i in balls) {
int randomAngle = arc4random() % 360;
CGPoint point1 = [self getVectorFromAngle: randomAngle AndMagnitude: (arc4random() % 40) + ((arc4random() % 20) + 15)];
i.body -> f = cpv(point1.x, point1.y);
}
}
(另外,这里是我的方法,从一个角度得到一个载体,我怀疑是造成问题):
angle = (angle/180.0) * M_PI;
float x = magnitude * cos(angle);
float y = magnitude * sin(angle);
CGPoint point = CGPointMake(x, y);
return point;
你可以在计时器分析器下运行这个工具,看看什么是时间跨度。 – Metabble
嗯。是的,除非比你想象的要多得多,否则你不应该遇到性能问题。只有分析器会告诉你任何有趣的事情。 – slembcke