gpt4 book ai didi

ios - 如何打开分数以使用 SpriteKit 创建 SKEmitterNode?

转载 作者:搜寻专家 更新时间:2023-10-31 22:44:56 26 4
gpt4 key购买 nike

我希望能够根据分数打开和关闭我的 SKEmitterNode(雨粒子)。但是我的更新功能不断被调用,即我最终在屏幕上看到数百万个粒子,下面是我当前的代码……我如何构建我的代码,以便在达到分数时只调用一次雨粒子?

class GameScene: SKScene, SKPhysicsContactDelegate {

func setUpRain() {
if let rainParticle = SKEmitterNode(fileNamed: "Rain") {
rainParticle.position = CGPointMake(frame.size.width, frame.size.height)
rainParticle.name = "rainParticle"
rainParticle.zPosition = Layer.Flash.rawValue
worldNode.addChild(rainParticle)
}
}

func makeItRain() {
let startRaining = SKAction.runBlock {
self.setUpRain()
}
runAction(startRaining, withKey: "Rain")
}

func stopRaining() {
removeActionForKey("Rain")
worldNode.enumerateChildNodesWithName("rainParticle", usingBlock: { node, stop in
node.removeFromParent()
})

}

}

class PlayingState: GKState {


unowned let scene: GameScene //used to gain access to our scene

override func updateWithDeltaTime(seconds: NSTimeInterval) {
scene.updateForegroundAndBackground()
scene.updateScore()

if scene.score > 2 {
scene.makeItRain()

}

if scene.score > 4 {
scene.stopRaining()
}
}

最佳答案

有几种方法可以做到这一点,但其中最简单的方法是每次切换只调用一次 makeItRain() 或 stopRaining() 。我的意思是一旦调用 makeItRain,在调用 stopRaining 之前不能再次调用它。这可以用这样的 bool 值来完成:

   var rainToggle: Bool = false; //True = Raining
override func updateWithDeltaTime(seconds: NSTimeInterval) {
scene.updateForegroundAndBackground()
scene.updateScore()

if (scene.score > 4){
scene.stopRaining()
rainToggle = false;
}
else if (scene.score > 2 && !rainToggle) {
scene.makeItRain()
rainToggle = true;

}
}

这只是有点低效,因为您无缘无故地在每一帧调用 stopRaining() ,但是它完成了工作并且很容易理解。另请注意,我必须翻转您的 if 语句出现的顺序(否则它将不起作用)。

关于ios - 如何打开分数以使用 SpriteKit 创建 SKEmitterNode?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39551555/

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