Reputation: 25
I am new to SpriteKit and am trying to learn by creating a game similar to the popular iOS game Snake vs Block (game screenshot ). I am not sure if I am setting up the game's physics the most efficiently.
I set the blocks to be affected by gravity, while the ball node is not. In the touchesMoved method, I set the ball node's position to the touch's x position (y never changes).
Once a collision is detected, gravity is set to zero vector, the leading ball is removed and last ball put in its place. Once the block is removed, I restore gravity.
However I am not sure how to keep several ball nodes connected to each other as in the game and make them follow the lead ball's position with lag.
Any advice on this?
Upvotes: 1
Views: 734
Reputation: 3995
You can connect them together as joints via SKPhysicsJointPin
. You are basically making a rope / chain, and there are a ton of examples for Box2d ropes (SpriteKit physics is Box2d).
Just add the latest ball as a pin-joint to the bottom:
Here are some references for you in addition to my answer:
convert objC to swift (more or less): https://objectivec2swift.com/#/home/converter/
(Github to project in link ): https://www.youtube.com/watch?v=7jWdcbmnmKQ
http://www.waveworks.de/howto-make-hanging-chains-sprite-kit-physics-joints/
https://developer.apple.com/documentation/spritekit/skphysicsjoint
Upvotes: 3