Swift 将名称设置为SKSpriteNode?
我正在使用以下代码为我的游戏中的玩家生成一个随机图像Swift 将名称设置为SKSpriteNode?,swift,sprite-kit,skspritenode,Swift,Sprite Kit,Skspritenode,我正在使用以下代码为我的游戏中的玩家生成一个随机图像 var playerTexture = [SKTexture]() playerTexture.append(SKTexture(imageNamed: “red”)) playerTexture.append(SKTexture(imageNamed: “blue”)) playerTexture.append(SKTexture(imageNamed: “green”))
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的随机纹理(红色、蓝色或绿色)
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)
但是,当我检查播放器的名称时,它会显示为以下之一,具体取决于选择的随机图像:
可选(“红色”(120 x 120)”)
可选(“\'blue\'(120 x 120)”)
可选(“‘绿色’(120 x 120)”)
我的图像称为“红色”、“蓝色”和“绿色”,位于Xcode中的Assets.xcsets文件夹中
我的图像是120 x 120,但为什么会出现这些长随机名称?如何将“红色”、“蓝色”或“绿色”仅设置为玩家的名称
我需要确切的名称为“红色”、“蓝色”或“绿色”,因为我想稍后使用player.name来实现不同的功能。可能有更好的方法来实现这一点,但这是我想到的第一件事-将playerTexture变量设置为一个元组数组,其中包含纹理和要赋予它的名称:
var playerTexture = [(SKTexture, String)]()
playerTexture.append((SKTexture(imageNamed: “red”), "red"))
playerTexture.append((SKTexture(imageNamed: “blue”), "blue"))
playerTexture.append((SKTexture(imageNamed: “green”), "green"))
let rand = Int(arc4random_uniform(UInt32(playerTexture.count)))
let chosenColor = playerTexture[rand].0 as SKTexture
let colorName = playerTexture[rand].1
let playerSkin = chosenColor
player = SKSpriteNode(texture: playerSkin)
player.size = CGSize(width: 50, height: 50)
player.position = location
self.addChild(player)
player.name = colorName
从
SKTexture
获取文件名的唯一方法是使用类的文本表示法
Swift使通过CustomStringConverable协议将自定义类转换为字符串变得容易(例如用于打印)。(与早期实施相比有了明显的改进)
这是一个例子:
class X : CustomStringConvertible {
var description: String{
return "This is class X"
}
}
print(X()) //output is: "This is class X"
因此,如果您做出以下决定:
print(SKTexture())
您在Swift 2.2中的实际输出为:
<SKTexture> '(null)' (0 x 0)
输出:
red.png
优势
-操作简单,您可以获取您的SKTexture使用的真实文件名
缺点
-这并不优雅,最后你从一个类中解析一个字符串,它可能会改变他的描述结构(例如,可以在描述中添加一个新元素,文件名可能位于位置2,而不是
之后的实际索引1),在未来的iOS版本或Swift版本中,但这也可能发生在代码的其他部分。如果您刚开始使用SpriteKit,我建议您创建SKSpriteNode的专用子类,并将所有行为和关注点保留在其中。随着时间的推移,你会发现整个游戏逻辑的实现和维护将更加简单
public class PlayerNode:SKSpriteNode
{
static let colorNames = ["red","blue","green"]
static let textures:[SKTexture] = PlayerNode.colorNames.map(){ SKTexture(imageNamed:$0) }
init(randomAtLocation location:CGPoint)
{
let rand = Int(arc4random_uniform(UInt32(PlayerNode.colorNames.count)))
super.init( texture: PlayerNode.textures[rand],
color: UIColor.clearColor(),
size: CGSize(width: 50, height: 50)
)
name = PlayerNode.colorNames[rand]
position = location
}
public required init?(coder aDecoder: NSCoder) { super.init(coder:aDecoder) }
}
因此,您的播放器初始化代码可以非常简单:
let player = PlayerNode(randomAtLocation:location)
self.addChild(player)
正确的。无法从
SKTexture
本身检索名称,因此需要存储用于创建名称的字符串。(简化的一种方法是仅存储字符串,并在指定chosenColor
时初始化SKTexture
内联)谢谢您的支持!这是最简单的方法!我是新来的编码,所以这是完美的。干杯这是不是:“PlayerNode.colorNames.map(){SKTexture(ImageName:$0)}”是贯穿所有纹理的魔法巫术?除了关于数组巫术的混乱之外,我还要补充一点,谢谢!代码架构技巧是无价的。非常感谢。如果你有时间的话。。。如何更改此行以反映正在加载的图像的大小,而不是任意更改?“size:CGSize(宽度:50,高度:50)”您可以使用super.init(纹理:)而不使用颜色/大小参数。我相信它将使用纹理的大小作为默认值。你可以在下一行设置颜色,但我认为默认情况下也会很清晰。太棒了!很难理解,但很精彩。你是否可以用这样的方法浏览一个文件夹,取出所有要使用的图像,并命名后续节点?我也试着给你一个答案:)
let player = PlayerNode(randomAtLocation:location)
self.addChild(player)