mercredi 2 novembre 2016

Swift 3 - if statement query

I have a query regarding an if statement which I was told to use to fix an issue I was having. My code is shown below:

var paddleRTouched = false
// the part of code below is inside the touchesBegan function
if !paddleRTouched {
            paddleRTouched = true
            let Rpaddle = self.childNode(withName: RpaddleName) as! SKSpriteNode
            let startPosition = CGPoint(x: 301.7, y: 87)
            let newPosition = CGPoint(x: 226.7, y: 87)
            let moveToNew = SKAction.move(to: newPosition, duration: 0.5)
            let moveToOld = SKAction.move(to: startPosition, duration: 0.5)
            let waitTime = SKAction.wait(forDuration: 0.5)
            let delay = SKAction.wait(forDuration: 1)
            let endDelay = SKAction.run {
                self.paddleRTouched = false
            }
            let sequence = SKAction.sequence([moveToNew,waitTime,moveToOld,delay,endDelay])
            Rpaddle.run(sequence)

After the node is touched I set the variable 'paddleRTouched' to true and run the entire sequence. However my question is as at the end of the sequence I have a 1 second delay which is ended by setting the variable back to false. During this second if the paddle is touched the sequence isn't ran and it doesn't move. My question is essentially what does the if statement 'if !paddleRTouched {' mean? I don't understand as it doesnt have any value (e.g if paddleRTouched == 1)? Also why does re-setting the variable back to false re-enable the paddle so that it can be touched again?

Aucun commentaire:

Enregistrer un commentaire