如何比较 2 个 SKSpritenode 的纹理

How to compare 2 textures of SKSpritenodes

我正在尝试检查碰撞时 bodyAA 的纹理是否命名为 "playerpc"。如果是,我想 运行 一个动作,但我不知道如何检查。

我现在使用的代码:

              var testnode = SKSpriteNode(imageNamed: "playerpc")
              print(testnode.texture)
              if bodyAA.texture == testnode.texture{
                  print("Yes the same")
              }
              else{
                  print(bodyAA.texture)
              }

这是控制台的结果:

Optional(<SKTexture> 'playerpc' (153 x 274))
Optional(<SKTexture> 'playerpc' (153 x 274))

所以应该是一样的吧!但是当它比较时,我的代码决定它不一样,我该如何解决这个问题?

texture 是可选的 SKTexture。所以要比较你应该打开它,然后根据描述检查:

if bodyAA.texture!.description == testnode.texture!.description{
                  print("Yes the same")
              }
              else{
                  print(bodyAA.texture)
              }