for循环出错

时间:2016-04-01 20:16:08

标签: ios swift for-loop sprite-kit

我正在使用SpriteKit制作一个游戏,我写了这个来制作一个动人的背景:

        let spaceMovement = SKAction.moveBy(CGVector(dx:0, dy:-bg1.frame.size.height), duration: NSTimeInterval(0.4 * bg1.frame.size.height))
        let resetSpace = SKAction.moveBy(CGVector(dx:0, dy:bg1.frame.size.height), duration: 0.0)
        let repeatSpaceMovement = SKAction.repeatActionForever(SKAction.sequence([spaceMovement, resetSpace]))

        for var i:CGFloat = 0; i < self.frame.size.height / (bg1.size.height); i++
        {
            let sprite = self.bg1;
            sprite.zPosition = -20
            sprite.position = CGPointMake(sprite.size.width/2, i * sprite.size.height)
            sprite.runAction(repeatSpaceMovement)
            self.addChild(sprite)

        }

但它给了我这个错误:

libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

1 个答案:

答案 0 :(得分:2)

我认为你错过了.frame,所以你最终除以0?也许你的意思是:

for var i:CGFloat = 0; i < self.frame.size.height / (bg1.frame.size.height); i++

另外,不要使用C ++样式循环。他们走了,他们很难看。这个怎么样?它的行为应该相同。

for i in (0..<self.frame.size.height / bg1.frame.size.height) {
    ...
}