Swift: 暂停无限滚动背景(即无限循环)
Swift: Pause endless scrolling background (i.e. infinite loop)
在我的游戏中,当 "endGame" 函数被调用时,我希望某些事情暂停,而其他事情保持 运行。我已经能够通过使用 "paused" 布尔值来完成大多数操作,但我无法让它在我的滚动背景中工作。我知道问题与后台操作如何包含在 for 循环中有关,但我不确定如何解决这个问题。这是我的代码示例:
class GameScene: SKScene {
var sun = SKSpriteNode()
var background = SKSpriteNode()
override func didMoveToView(view: SKView) {
sun = SKSpriteNode(imageNamed: "Sun")
sun.zPosition = -2
sun.setScale(0.8)
sun.position = CGPoint(x: self.frame.size.width * 0.632, y: self.frame.size.height * 0.90)
let rotate = SKAction.rotateByAngle(CGFloat(M_PI_2), duration: NSTimeInterval(10))
let rotateForever = SKAction.repeatActionForever(rotate)
sun.runAction(rotateForever)
addChild(sun)
let backgroundTexture = SKTexture(imageNamed: "Background")
backgroundTexture.filteringMode = .Nearest
let moveBackground = SKAction.moveByX(-backgroundTexture.size().width, y: 0, duration: NSTimeInterval(0.004 * backgroundTexture.size().width * 2.0))
let resetBackground = SKAction.moveByX(backgroundTexture.size().width, y: 0, duration: 0.0)
let moveBackgroundForever = SKAction.repeatActionForever(SKAction.sequence([moveBackground,resetBackground]))
for var i:CGFloat = 0; i < 4.0 + self.frame.size.width / ( backgroundTexture.size().width ); ++i {
background = SKSpriteNode(texture: backgroundTexture)
background.setScale(0.5)
background.zPosition = -1
background.position = CGPointMake(i * background.size.width, backgroundVert)
background.runAction(moveBackgroundForever)
addChild(background)
}
}
func endGame {
sun.paused = true
background.paused = true
}
}
在此示例中,"paused" 布尔值非常适用于名为 "sun" 的旋转 SKSpriteNode,但不适用于背景。有什么想法吗?
看起来您正在 for 循环中创建多个背景。但是你只暂停了一个。
你应该做的是为你的背景精灵创建 1 SKNode
,将所有背景节点添加到这个节点,然后将主节点添加到你的场景。然后需要暂停的时候,只暂停master节点,然后master节点会暂停它的所有children
for var i:CGFloat = 0; i < 4.0 + self.frame.size.width / (backgroundTexture.size().width ); ++i {
var background = SKSpriteNode(texture: backgroundTexture)
background.setScale(0.5)
background.zPosition = -1
background.position = CGPointMake(i * background.size.width, backgroundVert)
background.runAction(moveBackgroundForever)
self.background.addChild(background)
}
addChild(background)
在我的游戏中,当 "endGame" 函数被调用时,我希望某些事情暂停,而其他事情保持 运行。我已经能够通过使用 "paused" 布尔值来完成大多数操作,但我无法让它在我的滚动背景中工作。我知道问题与后台操作如何包含在 for 循环中有关,但我不确定如何解决这个问题。这是我的代码示例:
class GameScene: SKScene {
var sun = SKSpriteNode()
var background = SKSpriteNode()
override func didMoveToView(view: SKView) {
sun = SKSpriteNode(imageNamed: "Sun")
sun.zPosition = -2
sun.setScale(0.8)
sun.position = CGPoint(x: self.frame.size.width * 0.632, y: self.frame.size.height * 0.90)
let rotate = SKAction.rotateByAngle(CGFloat(M_PI_2), duration: NSTimeInterval(10))
let rotateForever = SKAction.repeatActionForever(rotate)
sun.runAction(rotateForever)
addChild(sun)
let backgroundTexture = SKTexture(imageNamed: "Background")
backgroundTexture.filteringMode = .Nearest
let moveBackground = SKAction.moveByX(-backgroundTexture.size().width, y: 0, duration: NSTimeInterval(0.004 * backgroundTexture.size().width * 2.0))
let resetBackground = SKAction.moveByX(backgroundTexture.size().width, y: 0, duration: 0.0)
let moveBackgroundForever = SKAction.repeatActionForever(SKAction.sequence([moveBackground,resetBackground]))
for var i:CGFloat = 0; i < 4.0 + self.frame.size.width / ( backgroundTexture.size().width ); ++i {
background = SKSpriteNode(texture: backgroundTexture)
background.setScale(0.5)
background.zPosition = -1
background.position = CGPointMake(i * background.size.width, backgroundVert)
background.runAction(moveBackgroundForever)
addChild(background)
}
}
func endGame {
sun.paused = true
background.paused = true
}
}
在此示例中,"paused" 布尔值非常适用于名为 "sun" 的旋转 SKSpriteNode,但不适用于背景。有什么想法吗?
看起来您正在 for 循环中创建多个背景。但是你只暂停了一个。
你应该做的是为你的背景精灵创建 1 SKNode
,将所有背景节点添加到这个节点,然后将主节点添加到你的场景。然后需要暂停的时候,只暂停master节点,然后master节点会暂停它的所有children
for var i:CGFloat = 0; i < 4.0 + self.frame.size.width / (backgroundTexture.size().width ); ++i {
var background = SKSpriteNode(texture: backgroundTexture)
background.setScale(0.5)
background.zPosition = -1
background.position = CGPointMake(i * background.size.width, backgroundVert)
background.runAction(moveBackgroundForever)
self.background.addChild(background)
}
addChild(background)