Thank you for enjoying Designers.how!
Please enter your email address to unlock the rest of this episode.
Don't want to see this? Become a Designers.how member now.
Please enter your email address to unlock the rest of this episode.
Don't want to see this? Become a Designers.how member now.
Time to do some more work in the GameScene and get these runes drawn on the board. Start by making a couple variables in the GameScene to help with the process:
var level: Level!
let runeSize = CGSize(width: 70, height: 70)
The first is of course a reference to the level, and then we make a CGSize
that holds the rune size for us to reference later. This is based on the actual size of the rune sprite images, and it will be good to have the size defined in one place in case that changes.
Next, we'll add a function to determine the point for any given slot on the board. We'll need this information to be able to draw each rune in the proper position. Basically we return a Y value ranging from -226 in intervals of the rune size plus a margin of 5 - the gap defined by our design.
func pointFor(slot: Int) -> CGPoint {
return CGPoint(x: 0.0, y: -226 + CGFloat(slot) * (runeSize.height + 5))
}
Now it's possible to write the function that draws the runes on the game board. We make a for loop that creates a sprite for each rune in the supplied array (we'll later reference the runes in the level). Note the usage of our pointFor
function!
func drawRunes(for runes: [Rune]) {
for rune in runes {
let sprite = SKSpriteNode(imageNamed: "\(rune.runeColor)_\(rune.runeType)")
sprite.size = runeSize
sprite.position = pointFor(slot: rune.slot)
sprite.name = "rune"
addChild(sprite)
rune.sprite = sprite
}
}