簡體   English   中英

Spritekit 游戲中的無盡滾動(重復)背景 - Swift

[英]Endless scrolling (repeating) background in Spritekit game - Swift

我想為我的 spritekit 游戲創建一個無限滾動的背景,它可能應該由一兩個圖像組成,哪些圖像會重復? 我找到了這一個兩個例子,但它們在 obj 中。 C。

我不知道如何在 Swift 中實現這一點。是否可以手動設置速度?

ps:我沒有轉換obj的技能。 C 到 swift(新手到 Xcode dev.)

我找到了一種方法來做到這一點,不知怎的,我設法轉換這個 obj。 C迅速

您必須公開聲明這兩個節點

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

在“didMoveToView”方法中

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)

並在您添加的“覆蓋功能更新(currentTime:CFTimeInterval)”方法中

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)

            }

我不知道這是否是最有效的方式。 其他問題提到了For循環。 但在我看來這更容易。

我知道這場比賽已經晚了,但我也發現了如何橫向進行!

從Egghead的代碼開始(優秀的工作!)我修改了一些東西:

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

也:

    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)

並更新背景的位置:

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

我為此做了一堂課。 目前它是為Swift 5.0或4.2編寫的,它支持頂部,底部,左側和右側方向。

看看它是否有幫助,它叫做InfiniteScrollingBackground: https//github.com/ThiagoAM/InfiniteScrollingBackground

你不需要那一切。

只需將此函數與您聲明的變量以及您在didMoveToView中使用的屬性一起使用。

func backgroudScrollUpdate(){
    background1.position = CGPointMake(background1.position.x, background1.position.y - 1)
    background2.position = CGPointMake(background1.position.x, background2.position.y - 1)
    if background1.position.y == -UIScreen.mainScreen().bounds.height{
        background1.position = CGPointMake(0, 0)
        background2.position = CGPointMake(0, 0 + UIScreen.mainScreen().bounds.height)
    }
}

然后在您的更新方法中調用它。

當然,它不是最好的方式,因為它有兩個圖像可讀,當你有更多的循環。

在這里我提出了:

   func terrain() -> SKNode {
        let color = UIColor.init(red: randomColorChannel(), green: randomColorChannel(), blue: randomColorChannel(), alpha: 1.0)

        let scale: CGFloat = 1.0// UIScreen.mainScreen().scale
        let size = CGSizeMake(frame.size.width * scale, frame.size.height * scale)
        let node = SKSpriteNode.init(color: color, size: size)
        node.anchorPoint = CGPointZero
        node.position = CGPointMake(0, UIScreen.mainScreen().bounds.size.height)

        return node
    }

在更新中執行此操作:

override func update(currentTime: NSTimeInterval) {
        var newPosition1 = terrain1.position
        var newPosition2 = terrain2.position

        newPosition1.y -= terrainSpeed
        if newPosition1.y < 0 {
            newPosition2.y -= terrainSpeed
        }

        terrain1.position = newPosition1
        terrain2.position = newPosition2

        if terrain1.position.y <= -UIScreen.mainScreen().bounds.size.height {
            removeChildrenInArray([terrain1])
            terrain1 = terrain()
            addChild(terrain1)
        }

        if terrain2.position.y <= -UIScreen.mainScreen().bounds.size.height {
            removeChildrenInArray([terrain2])
            terrain2 = terrain()
            addChild(terrain2)
        }
    }

所以基本上,地形被放置在視圖上方,然后它們開始向下移動,並在需要時被新的替換,以重復。

    func backgroudScrollUpdate(){
        BG .position = CGPoint(x: BG.position.x - 5, y: BG.position.y)
        BG2.position = CGPoint(x: BG2.position.x - 5, y: BG2.position.y)
        if BG2.position.x <= -self.frame.size.width {
            BG.position = CGPoint(x: self.frame.size.width, y: 0)
        }
        if BG.position.x <= -self.frame.size.width {
        BG2.position = CGPoint(x: self.frame.size.width, y: 0)
    }
}

- 適用於SWIFT 3.1-

這是我一直用它來使它適用於Y軸只需將X位置的所有額外東西切換到Y位置

var backgroundImageWidth: CGFloat = 3000

override func didMove(to view: SKView) {
    self.anchorPoint = CGPoint(x: 0.5, y: 0.5)
    for i in 0...2 {
        let backgroundImage =  SKSpriteNode(imageNamed: "BackgroundImage")
        backgroundImage.name = "back"
        backgroundImage.size = CGSize(width: backgroundImageWidth, height: 1000)
        backgroundImage.anchorPoint = CGPoint(x: 0.5, y: 0.5)
        backgroundImage.position = CGPoint(x: CGFloat(i)*backgroundImage.size.width, y:0)
        self.addChild(backgroundImage)

    }
}

override func update(_ currentTime: TimeInterval) {
    // Called before each frame is rendered
    self.enumerateChildNodes(withName: "back", using: ({
        (node, error) in
        node.position.x -= 2
        if node.position.x < -(2 * self.backgroundImageWidth) {
            node.position.x += 3 * self.backgroundImageWidth
        }
                               
    }))
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM