skip to Main Content
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


  1. 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

    Login or Signup to reply.
  2. 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.

      enumerateChildNodes(withName: "ground") {
                       (node, _) in
                    let groundNode = node as! SKSpriteNode
                    let move = SKAction.moveBy(x: 1000, y: 1000, duration: 10)
                    groundNode.run(move)
      }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search