【发布时间】:2014-06-21 01:11:51
【问题描述】:
我有一个问题,我的 Sprite Kit 游戏只有在主角和任何其他精灵之间发生第一次碰撞时才会滞后。第一次碰撞发生后,其他所有碰撞都是平滑的,并且以 60.0 fps 的速度运行。奇怪的是,当第一次碰撞时,fps 只下降到 49-51,但实际游戏冻结了半秒。这也不是设置滞后的问题,因为无论我等待多长时间开始都会发生这种情况。有谁知道是什么问题?
-(void)checkForCollisions {
if (_invincible) return;
[self enumerateChildNodesWithName:@"enemy"
usingBlock:^(SKNode *node, BOOL *stop){
SKSpriteNode *enemy = (SKSpriteNode *)node;
CGRect smallerFrame = CGRectInset(enemy.frame, 22, 22);
if (CGRectIntersectsRect(smallerFrame, _sloth.frame)) {
[enemy removeFromParent];
[self runAction:[SKAction playSoundFileNamed:@"eagle.wav" waitForCompletion:NO]];
NSString *burstPath =
[[NSBundle mainBundle] pathForResource:@"ExplosionParticle" ofType:@"sks"];
SKEmitterNode *burstEmitter =
[NSKeyedUnarchiver unarchiveObjectWithFile:burstPath];
burstEmitter.position = _sloth.position;
[self addChild:burstEmitter];
[self changeInLives:-1];
_invincible = YES;
float blinkTimes = 10;
float blinkDuration = 3.0;
SKAction *blinkAction =
[SKAction customActionWithDuration:blinkDuration
actionBlock:
^(SKNode *node, CGFloat elapsedTime) {
float slice = blinkDuration / blinkTimes;
float remainder = fmodf(elapsedTime, slice);
node.hidden = remainder > slice / 2;
}];
SKAction *sequence = [SKAction sequence:@[blinkAction, [SKAction runBlock:^{
_sloth.hidden = NO;
_invincible = NO;
}]]];
[_sloth runAction:sequence];
}
}];
}
延迟与发射器节点无关,因为无论何时注释掉游戏仍然会延迟。 如果您需要任何其他信息,请告诉我。提前致谢! 这是我的 Instruments 跟踪文件的链接:https://www.dropbox.com/sh/xvd1xdti37d76au/ySL4UaHuOS 如果查看跟踪文件,请注意当 Time Profiler 达到 118% 时发生冲突。
【问题讨论】:
-
可能是在播放音频文件,需要在第一次播放时将其加载到内存中
-
谢谢!效果很好!
标签: ios opengl-es sprite-kit collision lag