gpt4 book ai didi

swift - 多个 SKSpriteNode 的 SpriteKit 多个 SKActions - 等待所有完成

转载 作者:可可西里 更新时间:2023-11-01 00:34:22 29 4
gpt4 key购买 nike

我正在开发一款 iOS 游戏,但遇到了问题。我需要显示 x 个 Sprite (对于每个 Sprite ,我都有一个比例 SKAction)。我需要能够等到所有 SKAction Sprite 出现,然后再做其他事情。每个 SKAction 在一个单独的线程中运行。我怎么能等?

这是一段代码:

for tile in tiles {

let randomNum:UInt32 = arc4random_uniform(20) // range is 0 to 99
let randomTime:TimeInterval = TimeInterval(randomNum/10)
let scale = SKAction.scale(by: 1, duration: 2, delay:randomTime , usingSpringWithDamping: 0.1, initialSpringVelocity: 5)

tile.sprite = SKSpriteNode(imageNamed: tile.type.spriteName)
tile.sprite?.size = CGSize(width: TileWidth, height: TileHeight)
tile.sprite?.position = tile.position!

tile.sprite?.scale(to: CGSize(width: 0, height: 0))
cookiesLayer.addChild(tile.sprite!)
tile.sprite?.run(scale)


}
//TODO code to add to be executed after all SKActions

我怎样才能让我的 TODO 代码在所有 SKActions 之后成为执行者?我想并行或一个接一个地运行 SKAction。

谢谢。

最佳答案

您可以在 run 方法中使用完成 block 非常轻松地完成此操作。

只是为了这个例子,假设您有一个名为 someSpriteNode 的 SKSpriteNode 并且想知道两个 Action (在这种情况下为 applyImpulse)何时完成运行:

    // 1) Create your actions:

let action1 = SKAction.applyImpulse(CGVector(dx: 1.0, dy: 0.0), duration: 2.0)
let action2 = SKAction.applyImpulse(CGVector(dx: 6.0, dy: 2.0), duration: 1.0)

// 2) Add them to a sequence:

let actionSequence = SKAction.sequence([action1, action2])

// 3) Run the sequence using a completion block:

someSpriteNode?.run(actionSequence, completion: {

// All your actions are now finished

// Do whatever you want here :)
})

更新:当执行一组操作时收到通知,其中所有操作都在同一节点上运行

那么您可能正在寻找操作组:

// Declare an empty array that will store all your actions:

var actions = [SKAction]()

// Iterate through your nodes:

for _ in 0..<6 {

// ...

// Generate your random scale, delay, or whatever you need:

let randomScale = CGFloat(GKRandomDistribution(lowestValue: 0, highestValue: 10).nextInt())

// Create your custom action

let scaleAction = SKAction.scale(by: randomScale, duration: 2.0)

// Append your action to the actions array:

actions.append(scaleAction)
}


// Create an action group using the actions array:

let actionGroup = SKAction.group(actions)

// Run your action group, and do whatever you need inside the completion block:

self.run(actionGroup, completion: {

// All your actions are now finished, no matter what node they were ran on.
})

此外,我建议您使用 GameplayKit 在您的游戏中生成随机数,它肯定会让您的生活更轻松:)

更新 2:在所有操作都在不同节点上运行的情况下,在执行所有操作时收到通知

使用DispatchGroup:

    // Create a DispatchGroup:

let dispatchGroup = DispatchGroup()

for _ in 0..<6 {

// ...

let randomWait = Double(GKRandomDistribution(lowestValue: 1, highestValue: 12).nextInt())

let waitAction = SKAction.wait(forDuration: randomWait)
let fadeOutAction = SKAction.fadeOut(withDuration: 2.0)
let fadeInAction = SKAction.fadeIn(withDuration: 2.0)
let sequenceAction = SKAction.sequence([waitAction, fadeOutAction, fadeInAction])

// Enter the DispatchGroup

dispatchGroup.enter()

colorSquares[i].run(sequenceAction, completion: {

// Leave the DispatchGroup

dispatchGroup.leave()
})

}

// Get notified when all your actions left the DispatchGroup:

dispatchGroup.notify(queue: DispatchQueue.main, execute: {

// When this block is executed, all your actions are now finished
})

我认为这个解决方案比计数器更优雅:)

关于swift - 多个 SKSpriteNode 的 SpriteKit 多个 SKActions - 等待所有完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46377475/

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