I am trying to do a for loop here using CGFloat but I am getting an error saying that
Type 'Range does not conform to protocol Sequence'
The code that I am trying to run is as below. The error happens at the "for" loop at the end of the code.
func setupBackgroundSea(){
//putting the background//
let texture = SKTexture(imageNamed: "background")
texture.filteringMode = .nearest
//calculating the number of background images needed//
let needNumber = 2.0 + (self.frame.size.width / texture.size().width)
//creating animation//
let moveAnim = SKAction.moveBy(x: -(texture.size().width), y: 0.0, duration: TimeInterval(texture.size().width/10.0))
let resetAnim = SKAction.moveBy(x: texture.size().width, y: 0.0, duration: 0.0)
let repeatForeverAnim = SKAction.repeatForever(SKAction.sequence([moveAnim,resetAnim]))
//setting the position of the image and the animation//
for var i:CGFloat in CGFloat(0)..<needNumber{
let sprite = SKSpriteNode(texture: texture)
sprite.zPosition = -100.0
sprite.position = CGPoint(x: i*sprite.size.width, y: self.frame.size.height/2.0)
}
I am quite new at Swift, so this might be a pretty noob question but I would appreciate it if anyone could help :)
Range<T>, unlike CountableRange<T> isn't a sequence, because it's unclear how to iterate it. Add 1 every time? 0.1? 0.001?
You can use stride instead:
for i in stride(from: 0 as CGFloat, to: needNumber, by: +1 as CGFloat) { //...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With