【问题标题】:Moving Background Swift SKSpriteKit移动背景 Swift SpriteKit
【发布时间】:2017-09-12 00:11:49
【问题描述】:

我目前正在开发一个尝试设置移动背景的应用程序。我有一个透明的图像,上面满是我目前正在使用的云。我的问题是,我怎样才能让它移动得更顺畅?我试过玩速度,但它看起来仍然很慢。任何帮助将是一种祝福。

这是我的视频。 http://sendvid.com/78ggkzcj

这是一张云图的图片。 Cloud Image

这是我的代码。你认为我应该改变什么或做不同的事情?

class GameScene: SKScene {

    // Background
    let background = SKSpriteNode(texture:SKTexture(imageNamed: "background"))

    // Clouds
    var mainCloud = SKSpriteNode()
    var cloud1Next = SKSpriteNode()

    // Time of last frame
    var lastFrameTime : TimeInterval = 0

    // Time since last frame
    var deltaTime : TimeInterval = 0

    override func didMove(to view: SKView) {
        background.position = CGPoint(x: 0, y: 0)

        // Prepare the clouds sprites
        mainCloud = SKSpriteNode(texture:
            SKTexture(imageNamed: "cloudbg1"))
        mainCloud.position = CGPoint(x: 0, y: 0)

        cloud1Next = mainCloud.copy() as! SKSpriteNode
        cloud1Next.position =
            CGPoint(x: mainCloud.position.x + mainCloud.size.width,
                    y: mainCloud.position.y)

        // Add the sprites to the scene
        self.addChild(background)
        self.addChild(mainCloud)
        self.addChild(cloud1Next)
    }

    override func update(_ currentTime: TimeInterval) {
        // Called before each frame is rendered
        // First, update the delta time values:

        // If we don't have a last frame time value, this is the first frame,
        // so delta time will be zero.
        if lastFrameTime <= 0 {
            lastFrameTime = currentTime
        }

        // Update delta time
        deltaTime = currentTime - lastFrameTime

        // Set last frame time to current time
        lastFrameTime = currentTime

        // Next, move each of the four pairs of sprites.
        // Objects that should appear move slower than foreground objects.
        self.moveSprite(sprite: mainCloud, nextSprite:cloud1Next, speed:100)
    }

    // Move a pair of sprites leftward based on a speed value;
    // when either of the sprites goes off-screen, move it to the
    // right so that it appears to be seamless movement
    func moveSprite(sprite : SKSpriteNode,
                    nextSprite : SKSpriteNode, speed : Float) -> Void {
        var newPosition = CGPoint.zero

        // For both the sprite and its duplicate:
        for spriteToMove in [sprite, nextSprite] {

            // Shift the sprite leftward based on the speed
            newPosition = spriteToMove.position
            newPosition.x -= CGFloat(speed * Float(deltaTime))
            spriteToMove.position = newPosition

            // If this sprite is now offscreen (i.e., its rightmost edge is
            // farther left than the scene's leftmost edge):
            if spriteToMove.frame.maxX < self.frame.minX {

                // Shift it over so that it's now to the immediate right
                // of the other sprite.
                // This means that the two sprites are effectively
                // leap-frogging each other as they both move.
                spriteToMove.position =
                    CGPoint(x: spriteToMove.position.x +
                        spriteToMove.size.width * 2,
                            y: spriteToMove.position.y)
            }
        }
    }
}

【问题讨论】:

    标签: swift sprite-kit skspritenode cgpoint


    【解决方案1】:

    您的代码看起来不错。因为您在模拟器中运行游戏,所以您的 fps 很低。如果在真机上运行,​​应该是流畅的。

    【讨论】:

    • 哇,我什至在发帖之前就应该尝试过。简单的故障排除错误,我的错。感谢帮助的人!至少我可以帮助其他人做一些类似于我正在用我的代码做的事情!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-08
    • 1970-01-01
    相关资源
    最近更新 更多