I have SKActions defined in my class, eg:
let CSound = SKAction.playSoundFileNamed("C3.mp3", waitForCompletion: false)
let DSound = SKAction.playSoundFileNamed("D3.mp3", waitForCompletion: false)
I want to be able to call a function to play sounds, and I want to adjust the name dynamically, like this:
func playSound(noteName: String) {
print(noteName) // "C"
var name = noteName + "Sound"
print(name) // "CSound"
run(SKAction(named: name)!)
}
This causes a fatal error on the run(SKAction(named: name)!) line:
Fatal error: Unexpectedly found nil while unwrapping an Optional value
Why is this? How is it nil? The variable name has the value CSound, and an SKAction does exist with that name. How can I use a dynamic name like this?