SpriteKit physics lags behind

I've been working on a SpriteKit game for a while now and have a frustrating issue that I can't get rid of. I don't want to talk too much about the game, but it's very simple. I generate several objects (SKSpriteNodes) every second that fall from the top of the screen using SpriteKits physics and the player interacts with them.

In most cases, the game works fine (constant 60 FPS). The problem is that sometimes (maybe once a minute or something) the game starts to lag a little by about 3-5 seconds (still at 60 FPS) and then it works fine (note: I run the game on iPhone 5s). It seems to be due to physics, because if I add a normal move action to the object, it works very smoothly while the physics-affected nodes lag behind.

I tried to remove some particles and effects that I have and I reuse my objects, but I cannot remove the lag. I decided to create a very simple test project to see if there would be a lag, but it's still there. Here is the code:

#import "GameScene.h"

static const uint32_t groundCategory = 1 << 0;
static const uint32_t objectCategory = 1 << 1;

@implementation GameScene

-(void)didMoveToView:(SKView *)view
    // Init
    self.backgroundColor = [UIColor blackColor];
    self.physicsWorld.gravity = CGVectorMake(0.0f, -3.2f);
    self.physicsWorld.contactDelegate = self;

    // Ground
    SKPhysicsBody* ground = [SKPhysicsBody bodyWithEdgeFromPoint:CGPointMake(0, -88) toPoint:CGPointMake(self.size.width, -88)];
    ground.categoryBitMask = groundCategory;
    self.physicsBody = ground;

    // Start game loop
    SKAction* waitAction = [SKAction waitForDuration:1];
    SKAction* sequence = [SKAction sequence:@[[SKAction performSelector:@selector(addObject) onTarget:self], waitAction]];
    SKAction* repeatAction = [SKAction repeatActionForever:sequence];
    [self runAction:repeatAction withKey:@"fallingObjectsAction"];

- (void)addObject
    SKSpriteNode* newObject = [SKSpriteNode spriteNodeWithColor:[UIColor whiteColor] size:CGSizeMake(88, 88)];
    newObject.name = @"Object";
    newObject.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:newObject.size];
    newObject.position = CGPointMake(CGRectGetMidX(self.frame), self.frame.size.height + newObject.size.height);
    newObject.physicsBody.categoryBitMask = objectCategory;
    newObject.physicsBody.contactTestBitMask = groundCategory;
    newObject.physicsBody.collisionBitMask = 0;

    [self addChild:newObject];

- (void)didBeginContact:(SKPhysicsContact *)contact
    if ([contact.bodyA.node.name isEqualToString:@"Object"])
        [contact.bodyA.node removeFromParent];
    else if ([contact.bodyB.node.name isEqualToString:@"Object"])
        [contact.bodyB.node removeFromParent];



I generate one object every second and let it fall from the top of the screen. When it hits the ground, it is removed.

Am I using physics incorrectly or is it a SpriteKit bug that it lags behind? It seems strange because I am running a very simple project using an iPhone 5 with iOS 8.


source to share

1 answer

Ok, I had the same problem with physics. The problem was that objects were volatile despite low CPU usage and consistent FPS. I finally figured out why. The solution I found does not set physicalWorld.speed to 1.0; set it to .9999. Everything now works smoothly.

Hope this helps.



All Articles