更新/更改 SKSpriteNode 的图像
Update / Change the image of SKSpriteNode
是否可以 update/change SKSpriteNode
的图像?
下面的代码不起作用:
var boss1 = SKSpriteNode(imageNamed: "boss1.png")
boss1 = SKSpriteNode(imageNamed: "boss2.png")
您现在正在做的是创建一个新实例,然后使用局部变量来引用它。然后,前一个的保留计数将降为零并被释放。你必须知道你的意图是什么。根据文档,我认为您可以更改它并且 属性 是 texture
:
This method creates a new texture object from the image file and
assigns that texture to the texture property. The size property of the
sprite is set to the dimensions of the image. The color property is
set to white (1.0,1.0,1.0).
更改方法如下:
boss1.texture = UIImage(...)
创建 SKSpriteNode
var boss1 = SKSpriteNode(imageNamed: "boss1.png")
你可以这样更新 texture
属性
boss1.texture = SKTexture(imageNamed: "boss2.png")
这是我的 Playground 中的示例
是否可以 update/change SKSpriteNode
的图像?
下面的代码不起作用:
var boss1 = SKSpriteNode(imageNamed: "boss1.png")
boss1 = SKSpriteNode(imageNamed: "boss2.png")
您现在正在做的是创建一个新实例,然后使用局部变量来引用它。然后,前一个的保留计数将降为零并被释放。你必须知道你的意图是什么。根据文档,我认为您可以更改它并且 属性 是 texture
:
This method creates a new texture object from the image file and assigns that texture to the texture property. The size property of the sprite is set to the dimensions of the image. The color property is set to white (1.0,1.0,1.0).
更改方法如下:
boss1.texture = UIImage(...)
创建 SKSpriteNode
var boss1 = SKSpriteNode(imageNamed: "boss1.png")
你可以这样更新 texture
属性
boss1.texture = SKTexture(imageNamed: "boss2.png")
这是我的 Playground 中的示例