LAST CHANCE: Save 50% on all my 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")


Hacking with Swift is sponsored by Essential Developer.

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until July 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Buy Pro Swift Buy Pro SwiftUI 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 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 Beyond Code

Was this page useful? Let us know!

Average rating: 1.0/5

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.