Swift无限背景不对齐左侧

时间:2017-02-21 03:08:58

标签: ios swift sprite-kit

这是我用来制作无限'的代码。通过每次创建一个新图像,然后不断更换旧图像的背景。但是,在应用程序开始时,只有一半的图像可见。我希望图像在启动时与屏幕左侧对齐,而不是在中心。

这个应用是横向的

 let bgTexture = SKTexture(imageNamed: "thebackground")
 let movebg = SKAction.moveBy(x: -bgTexture.size().width, y: 0, duration: 25)
 let replacebg = SKAction.moveBy(x: bgTexture.size().width, y: 0, duration: 0) //jumps the bg to original position
 let movebgForever = SKAction.repeatForever(SKAction.sequence([movebg, replacebg]))

        background.position = CGPoint(x: bgTexture.size().width/2, y: self.frame.midY)

        for var i in 0 ..< 3 {

                var loop:CGFloat = CGFloat (i) * bgTexture.size().width

                background = SKSpriteNode(texture: bgTexture)
                background.position = CGPoint(x: bgTexture.size().width/2 + loop, y: self.frame.midY)
                background.size.height = self.frame.height
                background.zPosition = -2
                background.run(movebgForever)


                self.addChild(background)
            }

1 个答案:

答案 0 :(得分:3)

我认为你的起始位置是错误的,行应该如下。 x位置需要为负,以与左边缘对齐。

background.position = CGPoint(x: -bgTexture.size().width/2 + loop, y: self.frame.midY)