Swift 游戏场景随着时间或分数改变背景?
Swift Game Scene change background with time or score?
我的应用程序中有一个背景图像,它从上到下移动并重复。
我希望在 x 时间后屏幕上显示不同的图像。
这样可以吗?
什么时候最好删除初始背景以避免层数过多?
override func didMove(to view: SKView) {
let bgTexture = SKTexture(imageNamed: "bg1.png")
let moveBGanimation = SKAction.move(by: CGVector(dx: 0, dy: -bgTexture.size().height), duration: 4)
let shiftBGAnimation = SKAction.move(by: CGVector(dx: 0, dy: bgTexture.size().height), duration: 0)
let moveBGForever = SKAction.repeatForever(SKAction.sequence([moveBGanimation, shiftBGAnimation]))
var i: CGFloat = 0
while i < 3 {
bg = SKSpriteNode(texture: bgTexture)
bg.position = CGPoint(x: self.frame.midX, y: bgTexture.size().height * i)
bg.size.width = self.frame.width
bg.zPosition = -2
bg.run(moveBGForever)
self.addChild(bg)
i += 1
}
bg2Timer = Timer.scheduledTimer(timeInterval: 3, target: self, selector: #selector(self.moveBG2), userInfo: nil, repeats: false)
}
func moveBG2() {
let bg2Texture = SKTexture(imageNamed: "bg2.png")
let moveBG2animation = SKAction.move(by: CGVector(dx: 0, dy: -bg2Texture.size().height * 2), duration: 8)
let shiftBG2Animation = SKAction.move(by: CGVector(dx: 0, dy: bg2Texture.size().height), duration: 0)
let moveBG2Forever = SKAction.repeatForever(SKAction.sequence([moveBG2animation, shiftBG2Animation]))
var j: CGFloat = 0
while j < 3 {
bg2 = SKSpriteNode(texture: bg2Texture)
bg2.position = CGPoint(x: self.frame.midX, y: bg2Texture.size().height + bg2Texture.size().height * j)
bg2.size.width = self.frame.width
bg2.zPosition = -1.5
bg2.run(moveBG2Forever)
self.addChild(bg2)
j += 1
}
}
这是我想出的一个主意。
重构这段代码:
let bgTexture = SKTexture(imageNamed: "bg.png")
let moveBGanimation = SKAction.move(by: CGVector(dx: 0, dy: -bgTexture.size().height), duration: 4)
let shiftBGAnimation = SKAction.move(by: CGVector(dx: 0, dy: bgTexture.size().height), duration: 0)
let moveBGForever = SKAction.repeatForever(SKAction.sequence([moveBGanimation, shiftBGAnimation]))
方法:
func runBgActions(bgTextureName: String) {
let bgTexture = SKTexture(imageNamed: bgTextureName)
bg.texture = bgTexture
let moveBGanimation = SKAction.move(by: CGVector(dx: 0, dy: -bgTexture.size().height), duration: 4)
let shiftBGAnimation = SKAction.move(by: CGVector(dx: 0, dy: bgTexture.size().height), duration: 0)
let moveBGForever = SKAction.repeatForever(SKAction.sequence([moveBGanimation, shiftBGAnimation]))
bg.removeAllActions()
bg.run(moveBGForever)
}
然后,创建另一个动作并 运行 它在不是 bg
的节点上,也许是场景本身。
let bg1Action = SKAction.run { runBgActions(bgTextureName: "bg1") }
let bg2Action = SKAction.run { runBgActions(bgTextureName: "bg2") }
let bg3Action = SKAction.run { runBgActions(bgTextureName: "bg3") }
let waitAction = SKAction.wait(forDuration: 10)
let sequence = SKAction.sequence([bg1Action, waitAction, bg2Action, waitAction, bg3Action, waitAction])
let repeatForeverAction = SKAction.repeatForever(sequence)
self.run(repeatForeverAction) // assuming self is the scene here
另一种方法是使用 SKAction.group
同时执行等待和移动后台操作,但我发现实现起来非常违反直觉。
在看到你的代码后,这就是我能够想出的:
var bg = SKSpriteNode()
func runBgActions(bgTextureName: String) {
let bgTexture = SKTexture(imageNamed: bgTextureName)
bg.texture = bgTexture
bg.position = CGPoint(x: self.frame.minX, y: self.frame.minY)
let moveBGanimation = SKAction.move(by: CGVector(dx: 0, dy: -(bgTexture.size().height - self.frame.height)), duration: 4)
let shiftBGAnimation = SKAction.move(by: CGVector(dx: 0, dy: bgTexture.size().height - self.frame.height), duration: 0)
let moveBGForever = SKAction.repeatForever(SKAction.sequence([moveBGanimation, shiftBGAnimation]))
bg.removeAllActions()
bg.run(moveBGForever)
}
override func didMove(to view: SKView) {
var i: CGFloat = 0
bg = SKSpriteNode(imageNamed: "bg1") // replace this with your first background's texture name
bg.position = CGPoint(x: self.frame.minX, y: self.frame.minY)
bg.anchorPoint = CGPoint(x: 0, y: 0)
bg.size.width = self.frame.width
bg.zPosition = -2
self.addChild(bg)
i += 1
let bg1Action = SKAction.run { self.runBgActions(bgTextureName: "bg1") }
let bg2Action = SKAction.run { self.runBgActions(bgTextureName: "bg2") }
let bg3Action = SKAction.run { self.runBgActions(bgTextureName: "bg3") }
let waitAction = SKAction.wait(forDuration: 10)
let sequence = SKAction.sequence([bg1Action, waitAction, bg2Action, waitAction, bg3Action, waitAction])
let repeatForeverAction = SKAction.repeatForever(sequence)
self.run(repeatForeverAction)
}
我的应用程序中有一个背景图像,它从上到下移动并重复。 我希望在 x 时间后屏幕上显示不同的图像。
这样可以吗?
什么时候最好删除初始背景以避免层数过多?
override func didMove(to view: SKView) {
let bgTexture = SKTexture(imageNamed: "bg1.png")
let moveBGanimation = SKAction.move(by: CGVector(dx: 0, dy: -bgTexture.size().height), duration: 4)
let shiftBGAnimation = SKAction.move(by: CGVector(dx: 0, dy: bgTexture.size().height), duration: 0)
let moveBGForever = SKAction.repeatForever(SKAction.sequence([moveBGanimation, shiftBGAnimation]))
var i: CGFloat = 0
while i < 3 {
bg = SKSpriteNode(texture: bgTexture)
bg.position = CGPoint(x: self.frame.midX, y: bgTexture.size().height * i)
bg.size.width = self.frame.width
bg.zPosition = -2
bg.run(moveBGForever)
self.addChild(bg)
i += 1
}
bg2Timer = Timer.scheduledTimer(timeInterval: 3, target: self, selector: #selector(self.moveBG2), userInfo: nil, repeats: false)
}
func moveBG2() {
let bg2Texture = SKTexture(imageNamed: "bg2.png")
let moveBG2animation = SKAction.move(by: CGVector(dx: 0, dy: -bg2Texture.size().height * 2), duration: 8)
let shiftBG2Animation = SKAction.move(by: CGVector(dx: 0, dy: bg2Texture.size().height), duration: 0)
let moveBG2Forever = SKAction.repeatForever(SKAction.sequence([moveBG2animation, shiftBG2Animation]))
var j: CGFloat = 0
while j < 3 {
bg2 = SKSpriteNode(texture: bg2Texture)
bg2.position = CGPoint(x: self.frame.midX, y: bg2Texture.size().height + bg2Texture.size().height * j)
bg2.size.width = self.frame.width
bg2.zPosition = -1.5
bg2.run(moveBG2Forever)
self.addChild(bg2)
j += 1
}
}
这是我想出的一个主意。
重构这段代码:
let bgTexture = SKTexture(imageNamed: "bg.png")
let moveBGanimation = SKAction.move(by: CGVector(dx: 0, dy: -bgTexture.size().height), duration: 4)
let shiftBGAnimation = SKAction.move(by: CGVector(dx: 0, dy: bgTexture.size().height), duration: 0)
let moveBGForever = SKAction.repeatForever(SKAction.sequence([moveBGanimation, shiftBGAnimation]))
方法:
func runBgActions(bgTextureName: String) {
let bgTexture = SKTexture(imageNamed: bgTextureName)
bg.texture = bgTexture
let moveBGanimation = SKAction.move(by: CGVector(dx: 0, dy: -bgTexture.size().height), duration: 4)
let shiftBGAnimation = SKAction.move(by: CGVector(dx: 0, dy: bgTexture.size().height), duration: 0)
let moveBGForever = SKAction.repeatForever(SKAction.sequence([moveBGanimation, shiftBGAnimation]))
bg.removeAllActions()
bg.run(moveBGForever)
}
然后,创建另一个动作并 运行 它在不是 bg
的节点上,也许是场景本身。
let bg1Action = SKAction.run { runBgActions(bgTextureName: "bg1") }
let bg2Action = SKAction.run { runBgActions(bgTextureName: "bg2") }
let bg3Action = SKAction.run { runBgActions(bgTextureName: "bg3") }
let waitAction = SKAction.wait(forDuration: 10)
let sequence = SKAction.sequence([bg1Action, waitAction, bg2Action, waitAction, bg3Action, waitAction])
let repeatForeverAction = SKAction.repeatForever(sequence)
self.run(repeatForeverAction) // assuming self is the scene here
另一种方法是使用 SKAction.group
同时执行等待和移动后台操作,但我发现实现起来非常违反直觉。
在看到你的代码后,这就是我能够想出的:
var bg = SKSpriteNode()
func runBgActions(bgTextureName: String) {
let bgTexture = SKTexture(imageNamed: bgTextureName)
bg.texture = bgTexture
bg.position = CGPoint(x: self.frame.minX, y: self.frame.minY)
let moveBGanimation = SKAction.move(by: CGVector(dx: 0, dy: -(bgTexture.size().height - self.frame.height)), duration: 4)
let shiftBGAnimation = SKAction.move(by: CGVector(dx: 0, dy: bgTexture.size().height - self.frame.height), duration: 0)
let moveBGForever = SKAction.repeatForever(SKAction.sequence([moveBGanimation, shiftBGAnimation]))
bg.removeAllActions()
bg.run(moveBGForever)
}
override func didMove(to view: SKView) {
var i: CGFloat = 0
bg = SKSpriteNode(imageNamed: "bg1") // replace this with your first background's texture name
bg.position = CGPoint(x: self.frame.minX, y: self.frame.minY)
bg.anchorPoint = CGPoint(x: 0, y: 0)
bg.size.width = self.frame.width
bg.zPosition = -2
self.addChild(bg)
i += 1
let bg1Action = SKAction.run { self.runBgActions(bgTextureName: "bg1") }
let bg2Action = SKAction.run { self.runBgActions(bgTextureName: "bg2") }
let bg3Action = SKAction.run { self.runBgActions(bgTextureName: "bg3") }
let waitAction = SKAction.wait(forDuration: 10)
let sequence = SKAction.sequence([bg1Action, waitAction, bg2Action, waitAction, bg3Action, waitAction])
let repeatForeverAction = SKAction.repeatForever(sequence)
self.run(repeatForeverAction)
}