我想创建一个垂直移动的滚动背景。
由于我想随时停止背景,我不希望它在Update:
函数中实现。
在我的代码中,背景垂直滚动,但仍然不完美。有时它随机弹出,或让整个场景下降到较低的FPS。
更新1:
像克里斯蒂安说的那样,我现在使用了Update
函数,但是用Bool确定它是否应该停止。
正如您所看到的,我加快了Scene
,但背景移动的速度不会改变。如何加快背景速度?
代码:
...
var Scroll:Bool!
...
self.runAction(SKAction.speedBy(2.0, duration: 25))
...
func addBackground(){
Background1 = SKSpriteNode(imageNamed: "Background.png")
Background1.size = CGSize(width: self.frame.size.width, height: self.frame.size.height)
Background1.position = CGPoint(x: self.frame.size.width / 2, y: self.frame.size.height / 2)
Background1.zPosition = 1
self.addChild(Background1)
Background2 = SKSpriteNode(imageNamed: "Background.png")
Background2.size = CGSize(width: self.frame.size.width, height: self.frame.size.height)
Background2.position = CGPoint(x: self.frame.size.width / 2, y: Background1.position.y - Background2.size.height)
Background2.zPosition = 1
self.addChild(Background2)
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
if (Scroll == true){
Background1.position = CGPointMake(Background1.position.x, Background1.position.y + 4)
Background2.position = CGPointMake(Background2.position.x, Background2.position.y + 4)
if(Background1.position.y > self.frame.size.height + (Background1.size.height / 2))
{
Background1.position = CGPointMake(self.frame.size.width / 2, Background2.position.y - Background1.size.height)
}
if(Background2.position.y > self.frame.size.height + (Background2.size.height / 2))
{
Background2.position = CGPointMake(self.frame.size.width / 2, Background1.position.y - Background2.size.height)
}
}
}
答案 0 :(得分:4)
您应该将其放在update
方法中。您可以使用布尔值来停止更新过程。例如:
var moveBackground = true
func stopBackgroundMovement(){
moveBackground = false
}
//In your update-method:
if moveBackground {
//move the background
Background.position.y += 1
}
如果要每x秒调用一次更新函数,可以使用更新方法中的NSTimeInterval
参数。