Adding coins
Coins are more fun if there are two value variations. We will create two coins:
A Bronze Coin, worth one coin
A Gold Coin, worth five coins
The two coins will be distinguishable by their color, as seen here:
Creating the coin classes
We only need a single Coin
class to create both denominations. Everything in the Coin
class should look very familiar at this point. To create the Coin
class, add a new file named Coin.swift
and then enter the following code:
import SpriteKit class Coin: SKSpriteNode, GameSprite { var initialSize = CGSize(width: 26, height: 26) var textureAtlas: SKTextureAtlas = SKTextureAtlas(named: "Environment") var value = 1 init() { let bronzeTexture = textureAtlas.textureNamed("coin-bronze") super.init(texture: bronzeTexture, color: .clear, size: initialSize) self.physicsBody = SKPhysicsBody(circleOfRadius: size.width / 2) self.physicsBody?.affectedByGravity = false } // A function to transform this coin into...