gpt4 book ai didi

ios - 制作垂直移动背景

转载 作者:行者123 更新时间:2023-11-30 12:33:22 25 4
gpt4 key购买 nike

我是 Spritekit 的初学者,我尝试让背景垂直移动
我确实知道怎么做这就是 Controller 中的内容

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

}

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

enter image description here

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

最佳答案

背景水平移动,因为 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, middle

        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)
<小时/>

我不确定,但我认为,如果您只想要一个 Action ,您可以删除该序列:

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

关于ios - 制作垂直移动背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43166209/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com