Endless scrolling (repeating) background in Spritekit game - Swift

后端 未结 6 1844
野的像风
野的像风 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:47

    I know this is late to the game, but I found how to do this horizontally as well!

    Starting off with Egghead's code (Excellent work!) I modified some things:

        let background1 = SKSpriteNode(imageNamed: "Street_Example")
        let background2 = SKSpriteNode(imageNamed: "Street_Example")
    

    Also:

        background1.position = CGPoint(x: frame.size.width / 2, y:frame.size.height / 2)
        background1.size = CGSize(width: frame.width, height: frame.height)
        background1.anchorPoint = CGPointZero
        background1.position = CGPointMake(0, 0)
        background1.zPosition = -15
        self.addChild(background1)
    
        background2.size = CGSize(width: frame.width, height: frame.height)
        background2.anchorPoint = CGPointZero
        background2.position = CGPointMake(background1.size.width - 1,0)
        background2.zPosition = -15
        self.addChild(background2)
    

    And to update the position of the background:

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

提交回复
热议问题