Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type 'Range<CGFloat> does not conform to protocol Sequence' (Swift 3) [duplicate]

Tags:

swift

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

like image 294
sabrinazuraimi Avatar asked Nov 14 '25 11:11

sabrinazuraimi


1 Answers

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) { //...
like image 157
Alexander Avatar answered Nov 17 '25 09:11

Alexander