更新/更改SKSpriteNode的图像

时间:2015-12-21 22:45:04

标签: ios swift sprite-kit skspritenode sktexture

是否可以更新/更改SKSpriteNode的图像?
以下代码不起作用:

var boss1 = SKSpriteNode(imageNamed: "boss1.png")
boss1 = SKSpriteNode(imageNamed: "boss2.png")

2 个答案:

答案 0 :(得分:5)

创建SKSpriteNode

var boss1 = SKSpriteNode(imageNamed: "boss1.png")

您可以像这样更新texture属性

boss1.texture = SKTexture(imageNamed: "boss2.png")

以下是我的Playground

的示例

enter image description here

答案 1 :(得分:3)

您现在正在做的是创建一个新实例,然后使用局部变量来引用它。然后,前一个的保留计数将降至零并被取消分配。你必须知道你的意图是什么。根据文档,我认为您可以更改它,属性为texture

  

此方法从图像文件创建一个新的纹理对象   将该纹理指定给纹理属性。的大小属性   精灵设置为图像的尺寸。颜色属性是   设为白色(1.0,1.0,1.0)。

以下是更改方法:

boss1.texture = UIImage(...)

以下是链接:https://developer.apple.com/library/ios/documentation/SpriteKit/Reference/SKSpriteNode_Ref/#//apple_ref/occ/instp/SKSpriteNode/texture

相关问题