sdd
sdd

Reputation: 877

How to compare 2 textures of SKSpritenodes

I am trying to check at a collision if the texture of bodyAA is named "playerpc". If it is, I want to run an action, but I can't figure out how to check.

The code i am using right now:

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

This is the outcome from the console:

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

So it should be the same! but when it compares, my code decides it is not the same, how can I fix this?

Upvotes: 1

Views: 266

Answers (1)

William Kinaan
William Kinaan

Reputation: 28819

texture is optional SKTexture. So to compare you should unwrap it, and check according to the description like this:

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

Upvotes: 3

Related Questions