I’m working with two SKSpriteNode objects and was wondering how to draw a simple line between them. The line should be updating dynamically when objects are moving. I found a simple solution with a SKShapeNode class. Here is the code.
1. Create a SKPhysicsJoint object e.g. SKPhysicsJointSpring and connect objects with each other. Later add this joint to the physicsWorld:
SKPhysicsJointSpring* jointSpring = [SKPhysicsJointSpring jointWithBodyA: [self.actionLayer childNodeWithName: @"pixel"].physicsBody
bodyB: [self.actionLayer childNodeWithName: @"pixel2"].physicsBody
anchorA: CGPointMake(0.5, 0.5)
jointSpring.frequency = 1.0;
jointSpring.damping = 1.0;
[self.physicsWorld addJoint: jointSpring];
If you would like to make your characters come alive and you want do it very simply – here is an example!
First, create NSMutableArray – you need to store all your textures here and use it as a parameter for SKAction’s method animateWithTextures: timePerFrame:.
NSMutableArray *textureActionArray = [NSMutableArray array];
Of course it is necessary to add Texture Altas to the project. It should look like the screenshot below:
When two bodies are occupying the same place you can make a decision how your game responds. In Sprite Kit you have two kinds of interactions between physics bodies:
– A contact is used when you need to know when two bodies are touching each other.
– A collision is used when you need to decide if your body should strike another body or ignores it.
Contacts and collisions can work independent. It means that you can check contacts even when collisions are disabled.
The categorizations and interactions are each defined by a 32-bit mask. Whenever a potential interaction occurs, the category mask of each body is tested against the contact and collision masks of the other body.
If you want to provide this feature to your game you have to remember about few important things.