' SKNode&#39?;没有名为' position'的成员

时间:2014-11-02 12:52:47

标签: ios swift sprite-kit sknode

我做错了什么?我似乎无法解决这个问题。我尝试在后面添加感叹号标记:var thisBlock = self.childNodeWithName(block)

这给了我一个新的错误说法。 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++
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

childNodeWithName()会返回一个可选的SKNode?,您必须将其解包才能使用。我不知道为什么var thisBlock = self.childNodeWithName(block)!没有解决您的问题。我建议使用可选绑定if let)语法:

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
}

如果没有子节点,这还有一个不会崩溃的优点。它只是不会进入该区块。