我有一个符合花栗鼠物理的CADisplayLink,而且我的性能非常慢。我在CADisplayLink更新时调用的方法中添加了一个NSLog,平均每秒调用22次。我的印象是这个数字应该接近60。我将frameInterval设置为1,那么在完美的世界中,它应该是60fps吗?增量时间平均约为0.0167秒(1/ 60是0.0167,这让我更加困惑)。
我的屏幕周围只有四面墙,屏幕上只有八个圆形的主体,每次调用都会更新为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;发布于 2013-02-03 17:55:33
在我的故事板中,我在一个不同的UIViewController上有一个方法,它每0.1秒触发一次,并且没有关闭,并且结合物理处理,使事情陷入困境。
https://stackoverflow.com/questions/14650406
复制相似问题