【发布时间】:2016-07-18 15:43:16
【问题描述】:
我正在使用以下代码为我的游戏中的玩家生成随机图像。
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
上面的代码生成一个名为 playerSkin 的随机 SKTexture(红色、蓝色或绿色)。
player = SKSpriteNode(texture: playerSkin)
player.size = CGSize(width: 50, height: 50)
player.position = location
self.addChild(player)
player.name = chosenColor.description
然后代码的下一部分将播放器创建为 SKSpriteNode,并为其分配红色、蓝色或绿色的随机纹理。但主要的是代码将 player.name 分配给随机选择的图像:
player.name = chosenColor.description
然后我使用打印检查播放器的名称。
print(player.name)
但是,当我检查播放器的名称时,它会根据选择的随机图像显示为以下之一:
可选的("\'red\' (120 x 120)")
可选(“\'蓝色\'(120 x 120)”)
可选(“\'绿色\'(120 x 120)”)
我的图像被称为“红色”、“蓝色”和“绿色”,位于 Xcode 的 Assets.xcassets 文件夹中。
我的图像是 120 x 120,但为什么会出现这些长随机名称?如何将“red”、“blue”或“green”设置为仅播放器的名称?
我需要将名称完全设为“红色”、“蓝色”或“绿色”,因为我想稍后将 player.name 用于不同的功能。
【问题讨论】:
标签: swift sprite-kit skspritenode