Question

I have a problem in which my Sprite Kit game lags only when the first collision occurs between the main character and any other sprite. After the first collision occurs, every other collision is smooth and runs at 60.0 fps. The odd thing is that when the first collision, the fps only drops to 49-51, but the actual game freezes for a half second. This is also not an issue of setup lag as this occurs no matter how long I wait to start. Does anyone know what the issue is?

-(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];

                           }
                       }];

}

The lag is not linked to the emitter node as the game still lags whenever it is commented out. Let me know if you need any additional information. Thanks in advance! Here is a link for my Instruments's trace file: https://www.dropbox.com/sh/xvd1xdti37d76au/ySL4UaHuOS If you look at the trace file, note that the collision occurs when the Time Profiler reaches 118%.

Was it helpful?

Solution

As Cocos mentioned, it is probably the initial loading of your sound file which is causing the one time delay.

In your implementation add the sound action:

SKAction *eagleSound;

In your init method add this:

eagleSound = [SKAction playSoundFileNamed:@"eagle.wav" waitForCompletion:NO];

Then whenever you need to play the sound, use this:

[self runAction:eagleSound];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top