Facundo Schiavoni
Facundo Schiavoni

Reputation: 461

Error with a for loop

I'm making a game in SpriteKit and I wrote this to make a moving background:

        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)

        }

but it gave me this error:

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

Upvotes: 0

Views: 123

Answers (1)

Eric Kuehne
Eric Kuehne

Reputation: 21

I think you are missing a .frame, so you end up dividing by 0? Maybe you meant:

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

Also, don't use C++ style loops. They are going away and they are ugly. How about this? It should behave identically.

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

Upvotes: 2

Related Questions