【问题标题】:Vertical endless scrolling background in sprite kit精灵套件中的垂直无限滚动背景
【发布时间】:2015-03-13 00:22:53
【问题描述】:

我正在制作一个游戏,我想让我的背景无限地垂直滚动,但我不知道如何开始。我对编码和精灵套件非常陌生,尤其是所以任何帮助将不胜感激。

有人知道我该怎么做吗?

【问题讨论】:

    标签: ios objective-c xcode scroll sprite-kit


    【解决方案1】:

    您希望有两个节点,一个开始,一个跟随它。最后,永远循环。

     func createGroundForward() {
            let groundTexture = SKTexture(imageNamed: "Track")
    
            for i in 0 ... 1 {
                let ground = SKSpriteNode(texture: groundTexture)
                ground.zPosition = -4
                ground.position = CGPoint(x: 0, y: -groundTexture.size().height + (groundTexture.size().height   + (groundTexture.size().height * CGFloat(i))))
    
                addChild(ground)
    
                let moveLeft = SKAction.moveBy(x:0 , y: -groundTexture.size().height, duration: 5)
                let moveReset = SKAction.moveBy(x:0 , y: groundTexture.size().height, duration: 0)
                let moveLoop = SKAction.sequence([moveLeft, moveReset])
                let moveForever = SKAction.repeatForever(moveLoop)
    
                ground.run(moveForever)
            }
        }
    

    然后将该函数添加到您的 didMove 函数(类似于 Sprite 的 viewDidLoad)。

     override func didMove(to view: SKView) {
            self.anchorPoint = CGPoint(x: 0.5, y: 0.5)
    
            createGroundForward()
        }
    

    【讨论】:

      【解决方案2】:

      我会在概念上给你一个答案,给你一个起点,因为你没有提供任何代码。

      您将需要多个与屏幕大小相同的图像,并将它们垂直堆叠在一起。

      然后您希望将这些图像逐点向下移动。当其中一个图像的顶部到达屏幕底部时,将其放回图像堆栈的顶部。这将导致您的背景无休止地滚动。

      祝你好运!

      【讨论】:

      • 啊,有道理!谢谢!但是,您会推荐多少份相同的图像作为一个不错的数量?
      • 这真的取决于你希望你的背景有多多样化。例如,如果在游戏中你驾驶飞机向前,背景垂直滚动,而你只有两张图片,一张是船,一张是岛,用户可能会意识到这只是一个重复的背景。但是如果你只有一个海洋,你可能只需要 2 个就可以逃脱,因为到处都是水。根据你的艺术和你想要达到的效果会有所不同,所以测试一下,看看什么适合你。