ios - 逐渐提高 SpriteKit 中滚动背景的速度

标签 ios swift sprite-kit

我正在用 SpriteKit 制作一个简单的游戏,我有一个滚动背景。简单的情况是,当游戏场景加载时,几张背景图像彼此相邻放置,然后当图像滚出屏幕时,图像水平移动。这是来 self 的游戏场景的 didMoveToView 方法的代码。

// self.gameSpeed is 1.0 and gradually increases during the game

let backgroundTexture = SKTexture(imageNamed: "Background")
var moveBackground = SKAction.moveByX(-self.frame.size.width, y: 0, duration: (20 / self.gameSpeed))
var replaceBackground = SKAction.moveByX(self.frame.size.width, y: 0, duration: 0)
var moveBackgroundForever = SKAction.repeatActionForever(SKAction.sequence([moveBackground, replaceBackground]))

for var i:CGFloat = 0; i < 2; i++ {
    var background = SKSpriteNode(texture: backgroundTexture)
    background.position = CGPoint(x: self.frame.size.width / 2 + self.frame.size.width * i, y: CGRectGetMidY(self.frame))
    background.size = self.frame.size
    background.zPosition = -100
    background.runAction(moveBackgroundForever)

    self.addChild(background)
}

现在我想在游戏的某些点提高滚动背景的速度。可以看到背景水平滚动的时长设置为(20/self.gameSpeed)。显然这是行不通的,因为这段代码只运行一次,因此移动速度永远不会更新以说明 self.gameSpeed 变量的新值。

所以,我的问题很简单:如何根据 self.gameSpeed 变量提高背景图片的移动速度(缩短持续时间)?

谢谢!

最佳答案

您可以使用 gameSpeed 变量来设置背景的速度。为此,首先,您需要引用您的两个背景片段(如果需要,可以引用更多):

class GameScene: SKScene {
    lazy var backgroundPieces: [SKSpriteNode] = [SKSpriteNode(imageNamed: "Background"), 
                                                 SKSpriteNode(imageNamed: "Background")]

    // ... 
}

现在您需要您的 gameSpeed 变量:

var gameSpeed: CGFloat = 0.0 {
    // Using a property observer means you can easily update the speed of the 
    // background just by setting gameSpeed.
    didSet {
        for background in backgroundPieces {
            // Minus, because the background is moving from left to right.
            background.physicsBody!.velocity.dx = -gameSpeed
        }
    }
}

然后在 didMoveToView 中正确定位每一 block 。此外,要使此方法起作用,每个背景片段都需要一个物理体,以便您可以轻松更改其速度。

override func didMoveToView(view: SKView) {
    for (index, background) in enumerate(backgroundPieces) {
        // Setup the position, zPosition, size, etc...

        background.physicsBody = SKPhysicsBody(rectangleOfSize: background.size)
        background.physicsBody!.affectedByGravity = false
        background.physicsBody!.linearDamping = 0
        background.physicsBody!.friction = 0

        self.addChild(background)
    }

    // If you wanted to give the background and initial speed,
    // here's the place to do it.
    gameSpeed = 1.0
}

您可以在 update 中更新 gameSpeed,例如 gameSpeed += 0.5

最后,在 update 中,您需要检查背景片段是否离开屏幕(向左)。如果有,则需要将其移至背景片段链的末尾:

override func update(currentTime: CFTimeInterval) {
    for background in backgroundPieces {
        if background.frame.maxX <= 0 {
            let maxX = maxElement(backgroundPieces.map { $0.frame.maxX })

            // I'm assuming the anchor of the background is (0.5, 0.5)
            background.position.x = maxX + background.size.width / 2
        }
    }
}

关于ios - 逐渐提高 SpriteKit 中滚动背景的速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30226379/

相关文章:

ios - 从Sprite Kit中的Update函数调用带有动画的方法

ios - 框架中的随机位置

ios - 构建在模拟器上成功但不在设备上运行

swift - 字典中可选值的意外 nil 检查结果

ios - 如何从父 UIStackView 引用和修改 subview UILabel

html - 实现 Scrollview 委托(delegate)方法时,WKWebview Scroll 变得缓慢和滞后

swift - uibackgroundTaskIdentifier - 应用程序关闭时 UIAlert 不会弹出

ios - 如何停止功能更新(_ currentTime : TimeInterval) in Spritekit

ios - Swift:NSString 无法转换为 'String'

ios - 如何跨 collectionview 添加 View ?