【问题标题】:Swift 3 - Background loop not working properlySwift 3 - 背景循环无法正常工作
【发布时间】:2017-03-24 15:41:49
【问题描述】:

我正在尝试制作类似 Flappy Bird 的游戏,并且必须让我的背景无限循环。我在网上找到了一些教程,但它对我不起作用,我不知道为什么。

这是我目前的代码

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)
    }

发生的情况是背景开始水平移动但也向上移动,我不明白为什么。

任何帮助将不胜感激!

【问题讨论】:

    标签: swift sprite-kit skaction


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-29
      • 1970-01-01
      • 1970-01-01
      • 2019-01-01
      相关资源
      最近更新 更多