How to get SKPhysicJoint -addJoint to work on a background thread

174 views Asked by At

I'm trying to load and initialize my scene on a background thread so that it doesn't pause for 4 seconds while it loads. It works just fine with one exception: I can't get SKPhysicsJointPin -addJoint to work - it crashes every time, but only if it's called while on the thread.

The joint is setup like so:

SKPhysicsJointPin* pin =[SKPhysicsJointPin jointWithBodyA:base.physicsBody bodyB:lever.physicsBody anchor:pt];

[gameScene.physicsWorld addJoint:pin];     // this crashes on a thread!

I'm doing the background loading like so:

gSceneBeingLoaded = self;                   // keep a strong ref while loading so that we don't release the object prematurely

dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void)
{
        /* LOAD SCENE ON BACKGROUND THREAD */

    [gSceneBeingLoaded loadSceneData];


            /* SHOW SCENE ON MAIN THREAD */

    dispatch_async(dispatch_get_main_queue(), ^(void)
    {
        [gSceneBeingLoaded presentScene];
        gSceneBeingLoaded = nil;                // release the extra reference
    });
});

Like I say, everything works great except for that one call which will crash every time, but only when on a thread.

2

There are 2 answers

1
Ahmet Hayrullahoglu On

This might solve your problem for addJoint:

SKPhysicsJointPin* pin =[SKPhysicsJointPin jointWithBodyA:base.physicsBody bodyB:lever.physicsBody anchor:pt];

GameScene *gameScene = (GameScene*)self.scene;
[gameScene.physicsWorld addJoint:pin];
1
djdance On

Another conclusion I found last night. Hope it will be useful for someone. dispatch_async NOT helps me.

If you create a POINT (SKShapeNode by path 1x1) and want to pin it - it is impossible in iOS8

3x3 - ok. Damn