【问题标题】:make vertical moving background制作垂直移动的背景
【发布时间】:2017-08-27 05:34:34
【问题描述】:

我是 Spritekit 的初学者,我尝试让背景垂直移动
我知道怎么做 这是控制器中的内容

class GameViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        let scene = GameScene(size: view.bounds.size)
        let skView = view as! SKView
        skView.showsFPS = true
        skView.showsNodeCount = true
        skView.ignoresSiblingOrder = true
        scene.scaleMode = .resizeFill
        skView.presentScene(scene)
    }

这个场景

class GameScene: SKScene {

var background = SKSpriteNode()
override func didMove(to view: SKView) {

    let backgroundTexture = SKTexture(imageNamed: "bbbgg.png")

    let shiftBackground = SKAction.moveBy(x: 0, y: -backgroundTexture.size().height, duration: 5)
    let replaceBackground = SKAction.moveBy(x: 0, y:backgroundTexture.size().height, duration: 0)
    let movingAndReplacingBackground = SKAction.repeatForever(SKAction.sequence([shiftBackground,replaceBackground]))

    for i in 1...3{
        background=SKSpriteNode(texture:backgroundTexture)
        background.position = CGPoint(x: 0, y: 0)
        background.size.height = self.frame.height
        background.run(movingAndReplacingBackground)

        self.addChild(background)
    }
}

}

问题是背景水平移动,到达屏幕末尾后消失,然后又开始从屏幕开头移动

我希望它垂直工作而不消失

【问题讨论】:

    标签: ios swift swift3 sprite-kit


    【解决方案1】:

    背景水平移动,因为 moveBy (shiftBackground, replaceBackground) 的声明适用于 x 轴(水平)。您需要处理 y 轴(垂直)。

        let shiftBackground = SKAction.moveBy(x: 0, y: -backgroundTexture.size().height, duration: 5)
        let replaceBackground = SKAction.moveBy(x: 0, y: backgroundTexture.size().height, duration: 0)
    

    在第一行,背景从它的位置移动到它的位置——它的高度。在第二行,它返回到它的第一个位置(我认为 moveBy 是相对的)。

    _

    背景再次移动,因为您使用了函数 SKAction.repeatForever (movingAndReplacingBackground)。我想你可以删除它。

        let movingAndReplacingBackground = SKAction.sequence([shiftBackground,replaceBackground])
    

    编辑:

    我忘记了循环。

    在循环中,你给背景一个初始位置。

                background.position = CGPoint(x: backgroundTexture.size().width/2 + (backgroundTexture.size().width * CGFloat(i)), y: self.frame.midY)
    

    如果您想要一个立即可见的背景,请尝试将其替换为 0,中间

            background.position = CGPoint(x: 0, y: self.frame.midY)
    

    或 -height, 0 如果您希望背景出现在屏幕顶部。在这种情况下你需要替换 shiftBackground 和 replaceBackground

            background.position = CGPoint(x: -backgroundTexture.size().height, y: 0)
    
        and
    
        let shiftBackground = SKAction.moveBy(x: 0, y: backgroundTexture.size().height, duration: 5)
    
        let replaceBackground = SKAction.moveBy(x: 0, y: 0, duration: 0)
    

    我不确定,但我认为,如果你只想要一个运动,你可以删除这个序列:

    class GameScene: SKScene {
    
        var background = SKSpriteNode()
        override func didMove(to view: SKView) {
    
            let backgroundTexture = SKTexture(imageNamed: "bbbgg.png")
    
            let scrollByTopBackground = SKAction.moveBy(x: O, y: backgroundTexture.size().height, duration: 5)
    
            background = SKSpriteNode(texture:backgroundTexture)
            background.position = CGPoint(x: -backgroundTexture.size().height, y: self.frame.midY)
            background.size.height = self.frame.height
            background.run(scrollByTopBackground)
    
            self.addChild(background)
        }
    }
    

    编辑回答你,试试这个...

    class GameScene: SKScene {
    
    var background = SKSpriteNode()
    override func didMove(to view: SKView) {
    
        let backgroundTexture = SKTexture(imageNamed: "bbbgg.png")
    
        let shiftBackground = SKAction.moveBy(x: 0, y: -backgroundTexture.size().height, duration: 5)
        let replaceBackground = SKAction.moveBy(x: 0, y:backgroundTexture.size().height, duration: 0)
        let movingAndReplacingBackground = SKAction.repeatForever(SKAction.sequence([shiftBackground,replaceBackground]))
    
        for i in 0...2 {
            background=SKSpriteNode(texture:backgroundTexture)
            background.position = CGPoint(x: 0, y: self.frame.midY + (backgroundTexture.size().height * CGFloat(i)))
            background.size.height = self.frame.height
            background.run(movingAndReplacingBackground)
    
            self.addChild(background)
        }
    }
    

    【讨论】:

    • 我做了这些更改,但现在什么都没有出现,而且我希望在运行应用程序时首先存在背景,然后开始移动
    • 不,我希望移动永远重复,因为我使用repeatForever 我在您发表评论后更新我的代码,现在问题:它是从上到下移动,但它从屏幕中间开始移动而不是从顶部开始,我也认为应用程序在移动之间停止了几秒钟,为什么?
    • 问题中的那个
    • 在你的第一个答案中你有这个 background.position = CGPoint(x: backgroundTexture.size().width/2 + (backgroundTexture.size().width * CGFloat(i)), y : self.frame.midY) 现在你有了 background.position = CGPoint(x: 0, y: 0) 我们可以尝试混合 background.position = CGPoint(x: 0, y: self.frame.midY)
    • 谢谢,这行得通!但是我仍然看到每个动作之间的空白,在下一个背景出现之前需要几秒钟,我希望在旧背景消失后立即出现下一个背景,而看不到它们之间的任何空格或空白,这可能吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多