SALE ENDS TODAY: Save 50% on all Swift books and bundles! >>

Optimizing SpriteKit physics

If you play our game on a real device, you’ll notice a problem: whenever new rocks are created off-screen, the game freezes briefly. It’s not a lot – maybe a tenth of a second depending on your device – but it’s still enough to be noticeable. You might also notice there’s a short delay the first time the player dies – try watching the particle system and you’ll see it’s far from smooth, at least the first time.

Both of these are optimization problems, and both can be fixed with a small amount of thinking. I know you might be tempted to move on to the next project, but trust me – spending a little time adding extra polish is good for you!

The reason our game pauses when rocks are created is because SpriteKit loads the rock texture and calculates pixel-perfect collisions for it. Although it’s an implementation detail – i.e., something we shouldn’t need to think about – SpriteKit will actually cache its textures for us, so although we can and should cache our rock texture it’s not the big problem here.

Instead, it’s that pesky pixel-perfect collision calculation: scanning the rock texture to see which pixels are transparent, so SpriteKit knows which parts we can crash into.

Now, if you think about it this pixel scanning doesn’t need to take place every time we create a new rock. In fact, it only needs to be run once: figure out the shape of the rock, then use copies of that physics body for all new rocks in the future. As all the rocks have the same shape, this means we do the work once and every future rock creation is effectively free.

To make that happen we need to add two new properties to our game scene: one to store the rock texture, and one to store the rock physics body. Add these now:

let rockTexture = SKTexture(imageNamed: "rock")
var rockPhysics: SKPhysicsBody!

Next, in didMove(to:) we need to create an SKPhysicsBody from our rock texture and store it in that rockPhysics property for later on:

rockPhysics = SKPhysicsBody(texture: rockTexture, size: rockTexture.size())

Now for the important part: now that we have our rock physics calculated, we should use that for the topRock and bottomRock physics bodies in the createRocks() method. Now, we can’t just assign it directly, because SpriteKit needs all its physics bodies to be unique. However, we can call copy() on our original to create a new instance, then typecast that as an SKPhysicsBody.

So, please modify the physics code in createRocks() to this:

topRock.physicsBody = rockPhysics.copy() as? SKPhysicsBody
bottomRock.physicsBody = rockPhysics.copy() as? SKPhysicsBody

That will remove the speed bump when creating new rocks, which means our game should stay nice and smooth while playing.

As for the other problem – the little speed bump when the player first dies – this is solved with another cache, this time for the player’s explosion. Even though this won’t be used in our game, this will force SpriteKit to preload the texture and keep it in memory, so it’s already there when it’s really needed.

So, please add this third property now:

let explosion = SKEmitterNode(fileNamed: "PlayerExplosion")


SPONSORED Instabug helps you identify and resolve severe crashes quickly. You can retrace in-app events and know exactly which line of code caused the crash along with environment details, network logs, repro steps, and the session profiler. Ask more questions or keep users up-to-date with in-app replies straight from your dashboard. Instabug takes data privacy seriously, so no one sees your data but you! See more detailed features comparison and try Instabug's crash reporting SDK for free.

Save 50% on all books and bundles

The biggest ever Hacking with Swift sale is now on, letting you save 50% on all books and bundles. Learn something new with Swift and enjoy great savings while the sale lasts!

Click here to save 50% in our Black Friday sale!

Buy Pro Swift Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift (Vapor Edition) Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Server-Side Swift (Kitura Edition) Buy Beyond Code

Was this page useful? Let us know!