Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 3-后台循环工作不正常_Swift_Sprite Kit_Skaction - Fatal编程技术网

Swift 3-后台循环工作不正常

Swift 3-后台循环工作不正常,swift,sprite-kit,skaction,Swift,Sprite Kit,Skaction,我正在尝试做一个像飞鸟一样的游戏,并且必须让我的背景无限循环。我在网上找到了一些教程,但它不适合我,我也不知道为什么 这是到目前为止我的代码 var Background = SKSpriteNode() var txtBG = SKTexture(imageNamed: "Background") var txtLand = SKTexture(imageNamed: "Ground") var Ground = SKSpriteNode() var moving:SKNode! // C

我正在尝试做一个像飞鸟一样的游戏,并且必须让我的背景无限循环。我在网上找到了一些教程,但它不适合我,我也不知道为什么

这是到目前为止我的代码

var Background = SKSpriteNode()
var txtBG = SKTexture(imageNamed: "Background")
var txtLand = SKTexture(imageNamed: "Ground")
var Ground = SKSpriteNode()
var moving:SKNode!


// Cena

override func didMove(to view: SKView) {

    scene?.anchorPoint = CGPoint(x: 0, y: 0)

    moving = SKNode()
    self.addChild(moving)


    //Ground

    Ground = SKSpriteNode(texture: txtLand)
    Ground.position = CGPoint(x:0, y: txtLand.size().height * 1.3)
    Ground.setScale(2.5)
    self.addChild(Ground)




    //Background

    Background = SKSpriteNode(texture: txtBG)
    Background.setScale(2.0)
    Background.position = CGPoint(x: self.size.width, y: txtLand.size().height * 5.2 )

    let BGmove = SKAction.moveBy(x: -txtBG.size().width * 2.0, y: txtLand.size().height * 5.2 , duration: TimeInterval(15))
    let BGRepo = SKAction.moveBy(x: txtBG.size().width * 2.0, y: txtLand.size().height * 5.2, duration: 0.0)
    let BGLoop = SKAction.repeatForever(SKAction.sequence([BGmove,BGRepo]))

    for i in 0 ..< 2 + Int(self.frame.size.width / ( txtBG.size().width * 2 )) {
        let i = CGFloat(i)
        Background = SKSpriteNode(texture: txtBG)
        Background.setScale(2.0)
        Background.position = CGPoint(x:txtBG.size().width/2 + txtBG.size().width * i * 2.0, y: txtLand.size().height * 5.2)
        Background.run(BGLoop)
        self.addChild(Background)
    }
var Background=SKSpriteNode()
var txtBG=SKTexture(图像名为:“背景”)
var txtLand=SKTexture(图像名为:“地面”)
var接地=SKSpriteNode()
移动:SKNode!
//塞纳
覆盖func didMove(到视图:SKView){
场景?.anchorPoint=CGPoint(x:0,y:0)
moving=SKNode()
self.addChild(移动)
//地面
地面=SKSpriteNode(纹理:txtLand)
Ground.position=CGPoint(x:0,y:txtLand.size().高度*1.3)
接地。设置刻度(2.5)
self.addChild(地面)
//背景
背景=SKSpriteNode(纹理:txtBG)
背景设置刻度(2.0)
Background.position=CGPoint(x:self.size.width,y:txtLand.size().height*5.2)
设BGmove=SKAction.moveBy(x:-txtBG.size().width*2.0,y:txtLand.size().height*5.2,持续时间:时间间隔(15))
设BGRepo=SKAction.moveBy(x:txtBG.size().width*2.0,y:txtLand.size().height*5.2,duration:0.0)
让BGLoop=SKAction.repeatForever(SKAction.sequence([BGmove,BGRepo]))
对于0中的i..<2+Int(self.frame.size.width/(txtBG.size().width*2)){
设i=CGFloat(i)
背景=SKSpriteNode(纹理:txtBG)
背景设置刻度(2.0)
Background.position=CGPoint(x:txtBG.size().width/2+txtBG.size().width*i*2.0,y:txtLand.size().height*5.2)
后台运行(BGLoop)
self.addChild(背景)
}
背景开始水平移动,但也向上移动,我不明白为什么


非常感谢您的帮助!

您的背景移动操作将背景移动x和y。如果您想使用
SKAction.moveBy(x:CGFloat,y:CGFloat,duration:TimeInterval)
则将y设置为0,或者您可以使用
SKAction.moveTo(x:CGFloat,duration:TimeInterval)
仅移动x值