user3482617 user3482617 - 1 month ago 11
iOS Question

Spritekit | Swift3 | Level Editor - Referencing the user data for SKSpritenodes from the Level Editor

In my level editor I have created a SKSpritenode with a name of "windmill".
To this I've added a user data attribute (rotation | String | left)

//I have no problem in loading the object in the GameScene class.
enumerateChildNodes(withName: "windmill") {windmillNode,_ in
windmill = windmillNode as? SKSpriteNode
windmillNode.zPosition = 3
windmillNode.move(toParent: _gameNode)
}


But what I want to do is call the user data property and depending on the rotation value. I want to set the angle.

--- Error "Cannot assign to immutable expression of type '_?'

Is the expression correct?

var angle: Int
if (windmillNode.userData?.object(forKey: "rotation")) = "left" {
angle = 360
} else {
angle = -360
}

Answer

Your single = is an assignment, you want to do a comparison so use ==. So your expression looks like

    let rotation = windmillNode.userData?.value(forKey: "rotation")
    if let rotateLeft = rotation as? String == "left" {

    }
Comments