I'm currently have an app with 6 balls. I use. the following method to move a Ball. Once it's touched I set its .isMoving property to true, and save the node that was touched into a global variable.
Of course the moment I get a touchesEnded or touchesCanceled, the first thing I do is set the moving property to false, clear the global variable and call a function that animates the ball back to its starting my point.
While this all works, I'm too new to Swift to know if this method has any flaws, or is not the proper/efficient way to do this.
If it makes any difference am catching all touches, and movements in the GameScene.
Of course the moment I get a touchesEnded or touchesCanceled, the first thing I do is set the moving property to false, clear the global variable and call a function that animates the ball back to its starting my point.
While this all works, I'm too new to Swift to know if this method has any flaws, or is not the proper/efficient way to do this.
If it makes any difference am catching all touches, and movements in the GameScene.
Code Block override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?){ guard let touch = touches.first else { return } let location = touch.location(in: self) let touchedNodes = self.nodes(at: location) for node in touchedNodes{ if let theNode = node as? SKSpriteNode { if theNode.name == "ball" { theNode.zPosition += 1 node.isMoving = true } } } } override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?){ guard touches.first != nil else { return } if let touch = touches.first, let node = myGlobalVars.currentBall, node.isMoving == true{ let touchLocation = touch.location(in: self) node.position = touchLocation //redraw node to emulate motion } }