如何在Swift中创建无尽的水平背景?

时间:2015-07-20 15:12:06

标签: swift sprite-kit

import SpriteKit

class GameScene: SKScene {

    let bgline1 = SKSpriteNode(imageNamed: "Line.png")
    let bgline2 = SKSpriteNode(imageNamed: "Line.png")

    override func didMoveToView(view: SKView) {
        /* Setup your scene here */
        backgroundColor = SKColor.whiteColor()
        bgline1.anchorPoint = CGPointZero
        bgline1.position = CGPointZero
        bgline1.zPosition = 1
        bgline1.size = CGSizeMake(self.frame.size.width, self.frame.size.height)
        self.addChild(bgline1)

        bgline2.anchorPoint = CGPointZero
        bgline2.position = CGPointMake(bgline1.position.x-1, 0)
        bgline2.zPosition = 1
        bgline2.size = CGSizeMake(self.frame.size.width, self.frame.size.height)
        self.addChild(bgline2)
    }

    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
        /* Called when a touch begins */

        for touch in (touches as! Set<UITouch>) {
            let location = touch.locationInNode(self)


        }
    }
    override func update(currentTime: CFTimeInterval) {

        /* Called before each frame is rendered*/
        bgline1.position = CGPoint(x: bgline1.position.x-4, y: bgline1.position.y)
        bgline2.position = CGPoint(x: bgline2.position.x-4, y: bgline2.position.y)

        if bgline1.position.x < -bgline1.size.width{
            bgline1.position = CGPointMake(bgline2.position.x + bgline2.size.width, bgline1.position.y)
        }
        if bgline2.position.x < -bgline2.size.width{
            bgline2.position = CGPointMake(bgline1.position.x + bgline1.size.width, bgline2.position.y)
        }
    }
}

这是我创建滚动水平无尽背景的代码。它应该是一个无尽的背景,但它不是。代码不是第一次尝试循环,但之后就是循环。我想知道为什么它不起作用,我应该如何解决它。

编辑 - 以2个节点和一行开始,fps <20。然后该行结束,当它结束时#34;节点计数变为0,fps变为60.比几秒钟后,该行弹回,并且它像无尽的滚动背景一样完美地运行。

1 个答案:

答案 0 :(得分:0)

bgline2.position = CGPointMake(bgline1.size.width-1, 0)

这行代码以某种方式解决了我的问题。我不知道它是如何解决它但确实如此。如果有人想为我解释一下,请做。我没有看到任何使用它的教程,但它对我有用。

罗希特夏尔