错误for循环CGFloat
我将一个项目从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的循环。所以你可以计算最后的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) }