我有一个简单的代码,可以使用 Spritekit 重复背景,我希望背景在发生任何触摸之前保持静止,并且仅在任何触摸开始时才开始动画或滚动
*// animate the background*
let background = SKTexture(imageNamed: "bg.png")
let moveTheBackground = SKAction.move(by: CGVector(dx: -
background.size().width, dy:0), duration: 10)
let backgroundToOriginalPosition = SKAction.move(by: CGVector(dx:
background.size().width, dy: 0), duration: 0)
let animateTheBackground =
SKAction.repeatForever(SKAction.sequence([moveTheBackground,
backgroundToOriginalPosition]))
*// loop the background animation*
var i:CGFloat = 0
while i < 2 {
bg = SKSpriteNode(texture: background)
bg.position = CGPoint(x: background.size().width * i, y: self.frame.midY)
bg.size.height = self.frame.height
bg.zPosition = -1
bg.run(animateTheBackground)
self.addChild(bg)
i += 1
}
我希望这段代码在 SpriteKit 中开始触摸时发生, 任何帮助将不胜感激
最佳答案
在任何游戏编程中,显示水平滚动的理想方式是使用 2 个背景图像并在水平方向上逐个添加。移动两者,每当背景1穿过最左边的0(从右到左滚动)时,将其位置更改为宽度为x。使用主游戏循环来增加两个背景位置。在您的情况下,使用一个名为allowScroll的标志,在touchesBegin时将其设置为true。如果allowScroll设置为true,则在游戏循环中增加/减少背景图像位置。
关于swift - 在 Spritekit 中触摸开始时重复背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45121365/