Tutorial by Examples

// World physics self.physicsWorld.gravity = CGVectorMake(0, -9.8);
Firstly, we set node category let groundBody: UInt32 = 0x1 << 0 let boxBody: UInt32 = 0x1 << 1 Then add Ground type node and Box type node. let ground = SKSpriteNode(color: UIColor.cyanColor(), size: CGSizeMake(self.frame.width, 50)) ground.position = CGPointMake(CGRectGetMidX(sel...
Set scene as delegate //set your scene as SKPhysicsContactDelegate class yourScene: SKScene, SKPhysicsContactDelegate self.physicsWorld.contactDelegate = self; Then you have to implement one or the other of the contact functions: optional func didBegin(contact:) and/or optional fund didEnd...
IF you are using simple categories, with each physics body belonging to only one category, then this alternative form of didBeginContact may be more readable: func didBeginContact(contact: SKPhysicsContact) { let contactMask = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask ...
Here is a simple Sprite-Kit GameScene.swift. Create a new, empty SpriteKit project and replace the GameScene.swift with this. Then build and run. Click on any of the objects on screen to give make them move. Check the logs and the comments to see which ones collide and which ones make contact. // ...
let bodies = (contact.bodyA.categoryBitMask <= contact.bodyB.categoryBitMask) ? (A:contact.bodyA,B:contact.bodyB) : (A:contact.bodyB,B:contact.bodyA) switch (bodies.A.categoryBitMask,bodies.B.categoryBitMask) { case let (a, _) where (a && superPower): //All we care about is i...
In Sprite-Kit, there is the concept of collisions which refers to the SK physics engine handling how physics objects interact when they collide i.e. which ones bounce off which other ones. It also has the concept of contacts, which is the mechanism by which your program gets informed when 2 physics...
For this example, we will used 4 bodies and will show only the last 8 bits of the bit masks for simplicity. The 4 bodies are 3 SKSpriteNodes, each with a physics body and a boundary: let edge = frame.insetBy(dx: 0, dy: 0) physicsBody = SKPhysicsBody(edgeLoopFrom: edge) Note that the 'ed...

Page 1 of 1