【问题标题】:How to scroll tiled sprites without gaps or glitches appearing inbetween in SpriteKit如何在 SpriteKit 中滚动平铺的精灵而不会出现间隙或故障
【发布时间】:2015-12-11 22:47:57
【问题描述】:

我在 SpriteKit 中有一个 SkSpriteNodes 的横向滚动平铺背景,并且偶尔会在平铺之间不断出现细小的间隙,因为它们没有准确地相互对接。我使用的代码是这样的;

func updateBackground() {

    worldNode.enumerateChildNodesWithName("Background", usingBlock: { node, stop in

        if let background = node as? SKSpriteNode {

            let scrollAmount = CGPoint(x: -self.kBackgroundSpeed * CGFloat(self.deltaTime), y: 0)
            background.position += scrollAmount

            if background.position.x < -background.size.width {

                background.position += CGPoint(x:background.size.width * CGFloat(self.kNumBackgroundSprites), y: 0)

            }

        }

    })

}

我需要对这段代码做些什么来防止出现空白?

【问题讨论】:

  • 您是否尝试过使用floorf() 之类的函数来确保您所有的精灵大小和位置都是像素对齐的?

标签: ios swift sprite-kit tvos


【解决方案1】:

如果您同时以相同的位移移动一堆节点,您应该考虑创建一个“容器”SKNode,在其中将所有这些节点添加为子节点并移动容器而不是单独移动每个节点。

【讨论】:

    【解决方案2】:

    为避免此类小错误,您应该考虑让 SpriteKit 使用 SKAction 为您移动背景。这样的事情应该可以解决问题:

    let moveLeft = SKAction.moveByX(-backgroundTexture.size().width, y: 0, duration: 20)
    let moveReset = SKAction.moveByX(backgroundTexture.size().width, y: 0, duration: 0)
    let moveLoop = SKAction.sequence([moveLeft, moveReset])
    let moveForever = SKAction.repeatActionForever(moveLoop)
    
    background.runAction(moveForever)
    

    如果您有两个像这样移动的背景,您应该能够相当轻松地获得无缝效果。正如您可能想象的那样,这种方法也比手动更新所有内容占用的 CPU 更少。

    我写了一个 Flappy Bird SpriteKit 教程来解决这个问题; you might find it interesting reading.

    【讨论】:

    • 感谢您的建议,但不幸的是这没有奏效!我在 tvOS 上使用完整的 1920x1080 背景图像。我不知道这是否与问题有关,但这些差距仍在出现,甚至转移到 SKActions。
    • Geoff:你能让它们重叠两点左右吗?这将有助于确定。我想知道你的作品中是否有一个非常细微的间隙。例如,Photoshop 喜欢在调整大小后使图像边缘略微半透明。
    猜你喜欢
    • 2019-09-22
    • 1970-01-01
    • 2011-06-26
    • 1970-01-01
    • 2019-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多