如果一个变量改变了它们的值,我如何改变 SpriteKit 对象的纹理。
类似于:
var x = 2
if x == 1 {sprite has texture1}
else if x == 2 {sprite has texture2}
最佳答案
如果 x
是一个属性,那么你可以像这样使用一个property observer
class Hero: SKSpriteNode {
var x: Int = 1 {
didSet {
switch x {
case 1: self.texture = SKTexture(imageNamed: "texture1")
case 2: self.texture = SKTexture(imageNamed: "texture2")
default: break
}
}
}
}
关于Swift:更改 Sprite Kit 对象的纹理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41065231/