【问题标题】:How to compare 2 textures of SKSpritenodes如何比较 SKSpritenodes 的 2 个纹理
【发布时间】:2015-10-05 11:08:28
【问题描述】:

我正在尝试检查 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))

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

【问题讨论】:

  • 因为你必须指出你在纹理中比较的是什么,因为它有它自己的属性。

标签: ios swift comparison textures sktexture


【解决方案1】:

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

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

【讨论】:

  • 我喜欢这个并在我的代码中使用它。由于纹理的大小取决于您使用的是非视网膜、2x 还是 3x 设备,并且我必须与硬编码的纹理名称进行比较,所以我与 if bodyAA.texture!.description.hasPrefix(" ... ") { 进行比较
猜你喜欢
  • 2019-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-25
  • 1970-01-01
  • 2012-03-13
  • 2013-07-05
相关资源
最近更新 更多