在我的游戏中,我想在调用“endGame”函数时暂停某些事情,而其他事情仍然在运行。我已经能够通过使用“暂停”布尔值来完成大多数操作,但我不能让它适用于我的滚动背景。我知道问题与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
}
}
在此示例中,“暂停”布尔值适用于旋转的SKSpriteNode,称为“sun”,但它不适用于背景。有什么想法吗?
答案 0 :(得分:2)
您应该为背景精灵创建1 SKNode
,将所有背景节点添加到此节点,然后将主节点添加到场景中。然后,当您需要暂停时,您只会暂停主节点,然后主节点将暂停其所有子节点
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)
答案 1 :(得分:0)
看起来你在for循环中创建了多个背景。但你只是暂停一个。