1

What am I doing wrong? I can't seem to figure this out. I have tried putting an exclamation mark behind: var thisBlock = self.childNodeWithName(block),

this gives me a new error saying. type () does not confirm to protocol 'BooleanType'.

func blockRunner() {
        for(block, blockStatus) in self.blockStatuses {
            var thisBlock = self.childNodeWithName(block)
            if blockStatus.shouldRunBlock() {
                blockStatus.timeGapForNextRun = random()
                blockStatus.currentInterval = 0
                blockStatus.isRunning = true
            }

            if blockStatus.isRunning {
                if thisBlock.position.x = blockMaxX{
                    thisBlock.position.x -= CGFloat(self.groundSpeed)
                } else {
                    thisBlock.position.x = self.origBlockPositionX
                    blockStatus.isRunning = false
                    self.score++
                    if ((self.score % 5) == 0) {
                        self.groundSpeed++
                    }
                    self.scoreText.text = String(self.score)
                }
            } else {
                blockStatus.currentInterval++
            }
        }
    }
CodeSmile
  • 64,284
  • 20
  • 132
  • 217
Jerom Kok
  • 11
  • 1

1 Answers1

0

childNodeWithName() does return an optional SKNode? which you have to unwrap to use. I don't know why var thisBlock = self.childNodeWithName(block)! didn't solve your issue. I would recommend using optional binding (if let) syntax:

if let thisBlock = self.childNodeWithName(block) {
    if blockStatus.shouldRunBlock() {
        blockStatus.timeGapForNextRun = random()
        blockStatus.currentInterval = 0
        blockStatus.isRunning = true
    }

    if blockStatus.isRunning {
        ... rest of your code
}

This has the added advantage of not crashing if there are no children nodes. It just won't enter the block.

vacawama
  • 150,663
  • 30
  • 266
  • 294