0

I converted a project from Swift 2 to Swift 3 and now I get an error in the following line:

for i:CGFloat in 0 ..< 2.0 + self.frame.size.width / ( skyTexture.size().width * 2.0 ) += 1 {

which says: left side of mutating operator isn't mutable '..<' returns immutable value

let skyTexture = SKTexture(imageNamed: "sky")
    skyTexture.filteringMode = .nearest

    let moveSkySprite = SKAction.moveBy(x: -skyTexture.size().width * 2.0, y: 0, duration: TimeInterval(0.1 * skyTexture.size().width * 2.0))
    let resetSkySprite = SKAction.moveBy(x: skyTexture.size().width * 2.0, y: 0, duration: 0.0)
    let moveSkySpritesForever = SKAction.repeatForever(SKAction.sequence([moveSkySprite,resetSkySprite]))

    for i:CGFloat in 0 ..< 2.0 + self.frame.size.width / ( skyTexture.size().width * 2.0 ) += 1 {
        let sprite = SKSpriteNode(texture: skyTexture)
        sprite.setScale(2.0)
        sprite.zPosition = -20
        sprite.position = CGPoint(x: i * sprite.size.width, y: sprite.size.height / 2.0 + groundTexture.size().height * 2.0)
        sprite.run(moveSkySpritesForever)
        moving.addChild(sprite)
    }
Bista
  • 7,869
  • 3
  • 27
  • 55
user6825883
  • 59
  • 10
  • 1
    What is your original code in Swift 2? – OOPer Oct 09 '16 at 07:12
  • 2
    how is this `2.0 + self.frame.size.width / ( skyTexture.size().width * 2.0 ) += 1` supposed to work? Try it without the `=` – Paulw11 Oct 09 '16 at 07:17
  • Edited code does not contain Swift 2 version. Were you using C-style for loop in the original code? Like `for var i: CGFloat = 0; i < ...` – OOPer Oct 09 '16 at 07:23
  • Related: [Binary operator '..<' cannot be applied to operands of type 'Int' and 'CGFloat'](http://stackoverflow.com/q/37401102/2976878) – Hamish Oct 09 '16 at 07:37
  • 2
    @user6825883 Why don't you just use for i in 0.. – alexburtnik Oct 09 '16 at 07:48

1 Answers1

3

In essence what you want is still a loop through integer values incremented by 1. So you can just calculate the last int value and use a simple for each loop:

for i in 0..<Int(self.frame.size.width / ( skyTexture.size().width * 2.0 )) + 2 {
    //do your stuff here
    sprite.position = CGPoint(x: CGFloat(i) * sprite.size.width, y: sprite.size.height / 2.0 + groundTexture.size().height * 2.0)
}
alexburtnik
  • 7,661
  • 4
  • 32
  • 70