1
votes

I'm trying to find a way to make two SKShapeNodes stick together when I drag one close to another. Is there a way to do this with the SpriteKit API?

For example: two magnetic blocks that stick together to form a longer block when I move one block close to another.

Thanks!

1
In iOS 8, Apple added some new physics functionality to SpriteKit. I am not an expert in their use, but you might want to check out SKFieldNode. Specific to your example, they have a Magnetic Field node. Many others, as well. I should also point out they demo them in the WWDC 2014 session "What's New in SpriteKit". Good luck! - justinpawela
Oh, and you can check out Apple's Guide to Simulating Physics - justinpawela

1 Answers

3
votes

Check the position of the 2 SKSpriteNodes in question in the update method. When they have reached the desired distance, you can make one a child of the other. Alternately, you can create an invisible node, covering both SKSpriteNodes and make them children of the invisible node.