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)
}
}
I have 3 sksprite nodes with the name "ground". But only one of them runs the action move. How do I make them all run the action move? Thank you for any help.
2
Answers
https://developer.apple.com/documentation/spritekit/sknode/1483024-enumeratechildnodes Is what you want. Or keep references to them in an array. Or name them ground1, ground2 etc
You want to use enumerateChildNodes, this cycles through all nodes and any nodes whose name matches the withName part, the containing code is run.
Any node we do find while searching, we just assign it to a constant called groundNode (can be any name), and make it an SKSpriteNode. From there, you can do anything SKSpriteNode related, in your case we run a moveBy action on it.