Given the following class,
class Spaceship {
var position: CGPoint! {
didSet {
node.position = position
}
}
var node: SKSpriteNode!
init(frame: CGRect) {
node = SKSpriteNode(
color: UIColor.red,
size: CGSize(
width: frame.size.width / 5,
height: frame.size.width / 5
)
)
self.position = CGPoint(x: frame.midX, y: frame.midY)
}
}
it looks as though the didSet
observer of the position
property does not get called.
Since the didSet
observer should set the SpriteKit node's position when the position
property gets modified, I thought that instead of using the same line of code contained within the didSet
block, I could trigger the latter instead, but it doesn't seem to work; when the scene gets created (in the main GameScene, which simply creates a Spaceship
objects and adds the spaceship.node
to the scene's node children), the node
's position seems to be 0; 0
.
Any clue on why does this happen? Thank you.
Update: although the defer
statement works, the assignment does not allow to make use of it.