I’ve not had an excuse, up to now, to play with SpriteKit
. Initially I was going to re-write Nonoku following the same basic implementation as I did before with custom UIView
elements. Whilst walking back from lunch, I wondered if I could come up with something that wouldn’t stretch my limited artistic abilities but still look nice. Adding a little more motion to it would be good but that wouldn’t really fit with UIView
and autolayout
. So, I decided to give SpriteKit
another look.
I’d taken a glance at it before but not in any real depth and I was pretty sure I’d missed some stuff in just following tutorials.
So, first step, get an SKView
in my GameViewController
class. Easy enough, I’m using a storyboard so just change the class of the View belonging to the UIViewController
to an SKView
. It’s still a UIView
when referencing it in code so I need to remember to cast it when using it: view as! SKView
.
override func viewDidLoad() {
super.viewDidLoad()
let spriteView = view as! SKView
spriteView.showsFPS = true
spriteView.showsDrawCount = true
spriteView.showsNodeCount = true
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let gameScene = GameScene(size: view.bounds.size)
let spriteView = view as! SKView
spriteView.presentScene(gameScene)
}
Easy enough. I also need something to show so I included a GameScene
object. This is just a starting point so I’ll start with drawing the board the tiles will be displayed on.
The board is a rounded rectangle, divided into a 10x10 grid by dashed lines. Since I don’t expect this to change I added the board outline, and the lines, to an SKEffectNode
with shouldRasterize
set to true
. This means it will render itself to a cached bitmap so it can be re-rendered quickly, instead of re-drawing each node every frame. The number of draw calls dropped from 20, to 1, with this change. Whilst optimising upfront is normally to be avoided, this is a small change and easily reversed. More importantly, it’s something to be remembered later. Considering how multiple items can be grouped together can bring noticeable performance improvements.