如何在 sprite kit 中引用多个同名的 sprite?
How do you reference multiple sprites with the same name in sprite kit?
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 节点。但其中只有一个 运行 是动作动作。我怎样才能让他们都运行动起来?感谢您的帮助。
https://developer.apple.com/documentation/spritekit/sknode/1483024-enumeratechildnodes就是你想要的。或者将对它们的引用保存在一个数组中。或者将它们命名为 ground1、ground2 等
您想使用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)
}
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 节点。但其中只有一个 运行 是动作动作。我怎样才能让他们都运行动起来?感谢您的帮助。
https://developer.apple.com/documentation/spritekit/sknode/1483024-enumeratechildnodes就是你想要的。或者将对它们的引用保存在一个数组中。或者将它们命名为 ground1、ground2 等
您想使用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)
}