Hi, guys!
I'am writing game with SpriteKit framework and I try to make object with several configurations so I have to use static to avoid init method and some boilerplate code with decoder. So I wrote code but I'am not really know if this code is acceptable for creation purpose or not but this code actually works fine.
class Island: SKSpriteNode {
static func populateIsland(at point: CGPoint) -> SKSpriteNode {
let islandImageName = configureIslandName()
let island = SKSpriteNode(imageNamed: islandImageName)
island.setScale(randomScaleFactor)
island.position = point
return island
}
fileprivate static func configureIslandName() -> String {
let distribution = GKRandomDistribution(lowestValue: 1, highestValue: 4)
let randomNumber = distribution.nextInt()
let imageName = "pic" + "\(randomNumber)"
return imageName
}
fileprivate static var randomScaleFactor: CGFloat {
let distribution = GKRandomDistribution(lowestValue: 7, highestValue: 10)
let randomNumber: CGFloat = CGFloat(distribution.nextInt()) / 10
return randomNumber
}
}
So actual example is:
let island = Island.populateIsland(at: CGPoint(x: 100, y: 100))
Thank you for your suggestions in advance!