From what I can tell, the else statement within the didSet observer for isDiscoTime should pause the video and remove any actions within that node. I would like to know if this actually works properly for anyone else. When I tap the disco ball, the A/V begins, but other touches on the disco ball seem to be disabled until the video runAction completes. I couldn’t get this observer to work in my code or the completed code included in chapter 12.
Any thoughts?
UPDATE: So, I found the problem. The interact function only sets isDiscoTime to true, therefore the else statement inside the didSet operation is never called. Once I add the ability to set isDiscoTime to false in the interact function, I am able to toggle the video on and off, BUT I get an overlap in the audio if I manually change the isDiscoTime because the video action completion handler still runs. So, my question is:
How do I force the video runAction to complete immediately on change, instead of carrying out the completion handler? (Because I still want the 5 sec completion handler if I choose not to manually toggle back to non disco mode. I know removeAllActions( ) may not revert some previous changes, and this is the case here. )
Here’s what I have:
private var isDiscoTime: Bool = false {
didSet {
video.hidden = !isDiscoTime
if isDiscoTime {
video.play()
runAction(spinAction)
video.runAction(SKAction.waitForDuration(5.0), completion: {self.isDiscoTime = false})
SKTAudio.sharedInstance().playBackgroundMusic("disco-sound.m4a")
} else {
video.pause()
SKTAudio.sharedInstance().playBackgroundMusic("backgroundMusic.mp3")
removeAllActions()
}
}
}
UPDATE: Well, apparently all I had to do was add “video.removeAllActions( )” in the else statement. Sorry for all the updates, but I thought this might be helpful to others who wanted to try this alternative. Not really sure why removeAllActions( ) by itself wasn’t sufficient, but here’s the finished code for any of you who would like to try the alternative (Note: you have to keep removeAllActions( ) as well, or the disco ball will spin continuously):
private var isDiscoTime: Bool = false {
didSet {
video.hidden = !isDiscoTime
if isDiscoTime {
video.play()
runAction(spinAction)
video.runAction(SKAction.waitForDuration(5.0), completion: {self.isDiscoTime = false})
SKTAudio.sharedInstance().playBackgroundMusic("disco-sound.m4a")
} else {
video.pause()
SKTAudio.sharedInstance().playBackgroundMusic("backgroundMusic.mp3")
video.removeAllActions()
removeAllActions()
}
}
}
func interact() {
if !isDiscoTime {
isDiscoTime = true
} else {
isDiscoTime = false
}
}