I am using the following code to generate a random image for the player in my game.
var playerTexture = [SKTexture]()
playerTexture.append(SKTexture(imageNamed: “red”))
playerTexture.append(SKTexture(imageNamed: “blue”))
playerTexture.append(SKTexture(imageNamed: “green”))
let rand = Int(arc4random_uniform(UInt32(playerTexture.count)))
let chosenColor = playerTexture[rand] as SKTexture
let playerSkin = chosenColor
The code above generates a random SKTexture (red, blue or green) called playerSkin.
player = SKSpriteNode(texture: playerSkin)
player.size = CGSize(width: 50, height: 50)
player.position = location
self.addChild(player)
player.name = chosenColor.description
Then this next part of the code creates the player as an SKSpriteNode and assigns it a random texture of red, blue or green. BUT the main things is the code assigns the player.name to the randomly chosen image using:
player.name = chosenColor.description
Then I check the name of the player using print.
print(player.name)
However, when I check the name of the player, it appears as one of the following depending on what random image was chosen:
Optional(" \'red\' (120 x 120)")
Optional(" \'blue\' (120 x 120)")
Optional(" \'green\' (120 x 120)")
My images are called "red", "blue" and "green" and are located in Assets.xcassets folder in Xcode.
My images are 120 x 120 but why are these long random names appearing? How can I set either "red", "blue" or "green" as the name of the player only?
I need the name to be EXACTLY "red", "blue" or "green" because I want to use the player.name later for a different function.