【问题标题】:How do you reference multiple sprites with the same name in sprite kit?如何在 sprite kit 中引用多个同名的 sprite?
【发布时间】:2021-04-05 20:59:54
【问题描述】:
import SpriteKit
var ground: SKSpriteNode?

class GameScene: SKScene {
    
   
    override func didMove(to view: SKView) {
        ground = self.childNode(withName: "ground") as? SKSpriteNode
        let move = SKAction.moveBy(x: 1000, y: 1000, duration: 10)
        ground?.run(move)
    
}
    
}

我有 3 个名为“ground”的 sksprite 节点。但只有其中一个运行动作动作。我如何让他们都运行动作?感谢您的帮助。

【问题讨论】:

    标签: ios swift xcode sprite-kit


    【解决方案1】:

    您想使用 enumerateChildNodes,这将遍历所有节点和名称与 withName 部分匹配的任何节点,运行包含代码。 我们在搜索时找到的任何节点,我们只需将其分配给一个名为 groundNode 的常量(可以是任何名称),并使其成为 SKSpriteNode。从那里,您可以执行任何与 SKSpriteNode 相关的操作,在您的情况下,我们对其运行 moveBy 操作。

      enumerateChildNodes(withName: "ground") {
                       (node, _) in
                    let groundNode = node as! SKSpriteNode
                    let move = SKAction.moveBy(x: 1000, y: 1000, duration: 10)
                    groundNode.run(move)
      }
    

    【讨论】:

      【解决方案2】:

      https://developer.apple.com/documentation/spritekit/sknode/1483024-enumeratechildnodes 是你想要的。或者将它们的引用保存在一个数组中。或者将它们命名为 ground1、ground2 等

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多