background moving

Hi i'm trying to move a background endlessly, but i have a little problem when i simulate the code.


This is my code


inside the function didMove(to view: SKView) :

  for i  in 0...1{
        let backgroundNEW = SKSpriteNode(imageNamed: "New_Background")
        backgroundNEW.size = self.size
        backgroundNEW.anchorPoint = CGPoint(x: 0.5 , y: 0)
        backgroundNEW.position = CGPoint(x: self.size.width  ,  y: self.size.height
        )
        backgroundNEW.zPosition = -1
        backgroundNEW.name = "test"
        addChild(backgroundNEW)
}}


i declare this 3 var

var lastUpdateTime : TimeInterval = 0
var deltaFrameTime : TimeInterval = 0
var amountToMovePerSecond : CGFloat = 200.0



and i call them in my function update ,


override func update(_ currentTime: TimeInterval){       
            if lastUpdateTime == 0{
                lastUpdateTime = currentTime
            }
            else {
                deltaFrameTime = currentTime - lastUpdateTime
                lastUpdateTime = currentTime
            }

            let amountToMoveBackground =  amountToMovePerSecond * CGFloat(deltaFrameTime)

             self.enumerateChildNodes(withName: "test" )
            {
                backgroundNEW, _ in
     
                backgroundNEW.position.x -= amountToMoveBackground
     
                if backgroundNEW.position.x < -self.size.height{
                    backgroundNEW.position.x += self.size.height*2
                }      
            }
      }

the thing is i have a grey screen and i dont know why https://gyazo.com/63ce327ce9bc0a14199f411ac187de25


can anyone help me whit this problem please

Replies

— Your code lines 18-19 look wrong. You're mixing x coordinates with the node/image height, which makes no sense unless something is rotated, somehow.


— Your background image needs to be twice the size of the screen (width or height, according to which way you're going), otherwise you'll expose some of the gray background almost all the time. Or, use two nodes with the same image/texture, with one "following" the other.


— This would be better handled as a repeated SKAction, rather than by moving the sprite manually at each update. It'll move more smoothly, and use less system resources. And be less code, I would expect.

i changed my code now my code is this

    var background = SKSpriteNode()
    var background2 = SKSpriteNode()


    override func didMove(to view: SKView) {

   background = SKSpriteNode(imageNamed: "New_Background")
        background.position = CGPoint(x: 0, y:0)
        background.zPosition = -2
    /
        background.size = CGSize(width:self.frame.size.width*2,height:self.frame.size.height*2)


background2 = SKSpriteNode(imageNamed: "New_Background")
        background2.position = CGPoint(x: 0, y:0)
     /
        background2.zPosition = -2
        background2.size = CGSize(width:self.frame.size.width*2,height:self.frame.size.height*2)


self.addChild(background)
        self.addChild(background2)
        self.addChild(background3)


vitesse = SKAction.repeatForever(SKAction.move(by: CGVector(dx: -self.frame.size.width, dy: 0), duration: 2))


        background.run(vitesse)
        background2.run(vitesse)
     
}

  override func update(_ currentTime: TimeInterval) {
        /
        if background.position.x <= -self.frame.size.width {
            background.position.x = self.frame.size.width * 2
            /
        }
        if background2.position.x <= -self.frame.size.width {
            background2.position.x = self.frame.size.width * 2
            /
        }
     
    }

and now i got this https://gyazo.com/22f08fdf408e9548dfd0bc870cfb9f23

Don't you need to start 'background2' from a different X position, so that it doesn't sit on top of 'background' all through the action?