gpt4 book ai didi

ios - Sprite Kit 背景滚动垂直

转载 作者:搜寻专家 更新时间:2023-11-01 06:23:30 25 4
gpt4 key购买 nike

我想创建一个垂直移动的滚动背景。因为我想随时停止后台,所以我不希望它在 Update: 函数中实现。

在我的代码中,背景垂直滚动,但仍然不完美。有时它会随机弹出,或者让整个场景降低 FPS。

更新 1:

正如 Christian 所说,我现在使用 Update 函数,但使用 Bool 来确定它是否应该停止。如您所见,我加快了 Scene 的速度,但 Background 移动的速度不会改变。我怎样才能更快地启动后台?

代码:

  ...
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)

}

}
}

最佳答案

你应该把它放在 update 方法中。您可以使用 bool 值来停止更新过程。例如:

var moveBackground = true

func stopBackgroundMovement(){
moveBackground = false
}

//In your update-method:
if moveBackground {
//move the background
Background.position.y += 1
}

如果你想每 x 秒调用一次更新函数,你可以使用更新方法中的 NSTimeInterval 参数。

关于ios - Sprite Kit 背景滚动垂直,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28965612/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com