2
votes
if animal.texture == SKTexture(imageNamed: "cowTexture") {
   //do stuff
}

Apparently, the second half of my if statement above is creating a new texture which means the condition is never true. I did come across an answer to this problem using an enum and a property observer, but surely there's a more efficient way?!? Thanks!

1
Why would you want to compare textures, that would be slow. Think about your problem and explain what you want to do to solve it. - Knight0fDragon
I'm only comparing two textures so I doubt speed is going to be an issue. My problem is that the final texture is randomly chosen from an array of textures. In the example above, I might want to play a cow sound if animal has a cow texture. I suppose I could try to do a match on the array, but because it's an array of textures I think I'll be right back where I started. - squarehippo10
why don't you just give it a variable saying it is a cow then , that is what userData is for - Knight0fDragon
I forgot about userData! That might work very well...thanks! - squarehippo10

1 Answers

0
votes

Since there is no other code to base it off of, I can only think that you need to assign the SKTexture to a variable for comparison. So you could do something like

if animal.texture = cowTexture {
    //Do stuff
}

Where cowTexture has the

SKTexture(imageNamed: "cowTexture")

assigned to it.

Again, I can't really give a definite answer since you are using variables that aren't in your post.