Endless scrolling (repeating) background in Spritekit game - Swift

后端 未结 6 1847
野的像风
野的像风 2021-02-06 02:20

I want to Create a endless scrolling background for my spritekit game, iT should consist of one or two images probably, which repeat themselves? I found these one and two exampl

6条回答
  •  一生所求
    2021-02-06 02:31

    I found a way to do this, somehow i managed to convert this obj. C to swift

    You have to declare the two node's publicly

    let background1 = SKSpriteNode(imageNamed: "bg1")
    let background2 = SKSpriteNode(imageNamed: "bg2") 
    

    In the "didMoveToView" method

    background1.anchorPoint = CGPointZero
    background1.position = CGPointMake(0, 0)
    background1.zPosition = -15
    self.addChild(background1)
    
    background2.anchorPoint = CGPointZero
    background2.position = CGPointMake(0, background1.size.height - 1)
    background2.zPosition = -15
    self.addChild(background2)
    

    And in the "override func update(currentTime: CFTimeInterval)" method you add

    background1.position = CGPointMake(background1.position.x, background1.position.y - 2)
    background2.position = CGPointMake(background2.position.x, background2.position.y - 2)
    
                if(background1.position.y < -background1.size.height)
                {
                    background1.position = CGPointMake(background2.position.x, background1.position.y + background2.size.height )
                }
    
                if(background2.position.y < -background2.size.height)
                {
                    background2.position = CGPointMake(background1.position.x, background2.position.y + background1.size.height)
    
                }
    

    i don't know if it's the most efficient way of doing this. The other questions mentioned a For loop. But this is easier in my opinion.

提交回复
热议问题