1

I created this SpriteKitNode:

let examp = examps[examps.count-1] // examps is a global array with SKNodes inside of it 

But when I try to change its texture I receive this error:

examp.texture = SKTexture(imageNamed: "examp2")

Value of type 'SKNode' has no member 'texture'

However, it has a value of 'texture' so I don't know what the solution is.

https://imgur.com/a/s1V0XZF

Go Fast
  • 23
  • 5
  • Are you sure that `examp` is declared as local constant? – vadian Mar 17 '19 at 16:23
  • no, it's not. it's taken from a global array with SKNodes. is that fine? I edited my code to what it actually is now, sry for that – Go Fast Mar 17 '19 at 16:30
  • Please try to understand the error. The base class `SKNode` doesn't have a member `texture`, only the subclass `SKSpriteNode` has it. – vadian Mar 17 '19 at 16:52

1 Answers1

0

examps is [SKNode]. This means that examp is SKNode, not SKSpriteNode even if examps[examps.count-1] happens to hold an SKSpriteNode.

In order to access the texture property, you need to cast the result of examps[examps.count-1] to an SKSpriteNode.

if let examp = examps[examps.count-1] as? SKSpriteNode {
    examp.texture = SKTexture(imageNamed: "examp2")
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • Thank you! I thought I could simply access the 'texture' property since it was included in my examp constant – Go Fast Mar 17 '19 at 16:43