for循环CGFloat出错

时间:2016-10-09 07:05:26

标签: ios swift swift3

我将项目从Swift 2转换为Swift 3,现在我在以下行中收到错误:

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

说: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)
    }

1 个答案:

答案 0 :(得分:3)

本质上你想要的仍然是一个循环通过整数值递增1.所以你可以计算最后一个int值并为每个循环使用一个简单的:

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)
}