Collision between two objects - ios

I have created a simple project but I have a problem in the collisions.
It's simple (ball moving and vertical line) but didn't figure out how to stop the ball if it is touched the line.
import SpriteKit
class GameScene: SKScene,SKPhysicsContactDelegate {
var rPipe = SKSpriteNode() // Left Pipe
var ball1 = SKSpriteNode() // Ball
enum ColliderType:UInt32 {
case Ball1 = 1
case Pipe = 2
}
override func didMoveToView(view: SKView) {
self.physicsWorld.contactDelegate = self
// Pipe
let rPipeTexture = SKTexture(imageNamed: "pipe_r.png")
rPipe = SKSpriteNode(texture: rPipeTexture)
rPipe.position = CGPoint(x: CGRectGetMaxX(self.frame)-50, y: CGRectGetMidY(self.frame)-30)
rPipe.physicsBody = SKPhysicsBody(rectangleOfSize: rPipeTexture.size())
rPipe.physicsBody?.dynamic = false
rPipe.physicsBody?.categoryBitMask = ColliderType.Pipe.rawValue
rPipe.physicsBody?.contactTestBitMask = ColliderType.Pipe.rawValue
rPipe.physicsBody?.collisionBitMask = ColliderType.Pipe.rawValue
self.addChild(rPipe)
// Ball
let ballTexture = SKTexture(imageNamed: "gBall.png")
ball1 = SKSpriteNode(texture: ballTexture)
ball1.position = CGPoint(x: CGRectGetMinX(self.frame)+675, y: CGRectGetMaxY(self.frame)-220)
ball1.physicsBody = SKPhysicsBody(circleOfRadius: ballTexture.size().height/2)
ball1.physicsBody?.dynamic = false
ball1.physicsBody?.categoryBitMask = ColliderType.Ball1.rawValue
ball1.physicsBody?.contactTestBitMask = ColliderType.Pipe.rawValue
ball1.physicsBody?.collisionBitMask = ColliderType.Pipe.rawValue
self.addChild(ball1)
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in (touches ) {
let location = touch.locationInNode(self)
if ball1.containsPoint(location) {
ball1.position.x = location.x
}
}
}
func didBeginContact(contact: SKPhysicsContact) {
print("Contact")
}

One of your collided objects's dynamic property should be set to true. Otherwise the collision will be ignored. After setting dynamic, you also need to set affectedByGravity to false because the ball should not be affected by the gravity.
ball1.physicsBody?.dynamic = true
ball1.physicsBody?.affectedByGravity = false

Related

How to drag and flick a node in SpriteKit while gravity is present?

With my current code, the node is extremely laggy, and moves or teleports in random directions for some reason when its flicked. How can i fix this, and also can someone explain why it is teleporting and moving to random places in the scene.
Also, is there anyway to allow the node to be moved only when it is dragged from its position, rather than being at the gesturerecognizer's coordinates at all times?
override func didMove(to view: SKView) {
let gestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(self.pan(_:)))
view.addGestureRecognizer(gestureRecognizer)
circleNode.physicsBody = SKPhysicsBody(circleOfRadius: 20)
self.physicsWorld.gravity = CGVector(dx: 0, dy: 0)
self.addChild(circleNode)
}
#objc func pan(_ recognizer: UIPanGestureRecognizer) {
if recognizer.state == .changed {
self.physicsWorld.gravity = CGVector(dx: 0, dy: 0)
var location = recognizer.location(in: self.view!)
location = self.convertPoint(fromView: location)
circleNode.position = location
}
if recognizer.state == .ended {
self.physicsWorld.gravity = CGVector(dx: 0, dy: -9.8)
let transformerX = 1024/self.view!.frame.size.width
let transformerY = 768/self.view!.frame.size.height
let velocity = recognizer.velocity(in: self.view)
circleNode.physicsBody?.applyForce(CGVector(dx: velocity.x * transformerX, dy: velocity.y * transformerY))
}
}
Here is some code I was playing around with. I'm able to drag and flick a spear (spear Image) and also "pop" a pig head. This is the whole GameScene.Remove the code you don't need. :)
import SpriteKit
import CoreMotion
class GameScene: SKScene, SKPhysicsContactDelegate {
enum CollisionTypes: UInt32{
case spear = 1
case wall = 2
case head = 4
}
var touchPoint: CGPoint = CGPoint()
var touching: Bool = false
override func didMove(to view: SKView) {
self.physicsBody = SKPhysicsBody(edgeLoopFrom: self.frame)
//Add contact delegate
physicsWorld.contactDelegate = self
self.backgroundColor = .white
self.addChild(spearNode)
self.addChild(headNode)
}
lazy var spearNode: SKSpriteNode = {
let node = SKSpriteNode(imageNamed: "spear2")
node.name = "Spear"
node.physicsBody = SKPhysicsBody(texture: node.texture!,
size: CGSize(width: node.frame.width , height: node.frame.height))
node.position = CGPoint(x:self.frame.midX , y:self.frame.midY)
node.physicsBody?.affectedByGravity = true
node.physicsBody?.allowsRotation = false
node.size = CGSize(width: node.frame.width , height: node.frame.height )
node.physicsBody?.categoryBitMask = CollisionTypes.spear.rawValue
node.physicsBody?.contactTestBitMask = CollisionTypes.head.rawValue
node.physicsBody?.collisionBitMask = CollisionTypes.head.rawValue
return node
}()
lazy var headNode: SKSpriteNode = {
let node = SKSpriteNode(imageNamed: "Pig")
node.name = "Pig"
node.physicsBody = SKPhysicsBody(texture: node.texture!,
size: CGSize(width: node.frame.width , height: node.frame.height))
node.position = CGPoint(x:self.frame.midX , y:self.frame.maxY - 100)
node.physicsBody?.affectedByGravity = true
node.physicsBody?.allowsRotation = false
node.size = CGSize(width: node.frame.width / 2 , height: node.frame.height / 2 )
node.physicsBody?.categoryBitMask = CollisionTypes.head.rawValue
return node
}()
func didBegin(_ contact: SKPhysicsContact){
guard let nodeA = contact.bodyA.node else {return}
guard let nodeB = contact.bodyB.node else {return}
print("Contacted")
if nodeA.name == "Pig" && nodeB.name == "Spear"{
nodeA.removeFromParent()
}
if nodeA.name == "Spear" && nodeB.name == "Pig"{
nodeB.removeFromParent()
}
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first!
let location = touch.location(in:self)
if spearNode.frame.contains(location) {
touchPoint = location
touching = true
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first!
let location = touch.location(in: self)
touchPoint = location
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
touching = false
}
override func update(_ currentTime: TimeInterval) {
physicsWorld.gravity = CGVector(dx:0, dy: -9.8)
if touching {
let dt:CGFloat = 1.0/60.0
let distance = CGVector(dx: touchPoint.x-spearNode.position.x, dy: touchPoint.y-spearNode.position.y)
let velocity = CGVector(dx: distance.dx/dt, dy: distance.dy/dt)
spearNode.physicsBody!.velocity=velocity
}
}
}
Why not simply impart a force to the object based upon the swipe gesture rather than turning off gravity, manually moving the object, and then turning on gravity again when the swipe is over?

Sprite Kit more than 1 time collision

my game is not this but like this:
import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate {
var sprite = SKSpriteNode()
override func didMoveToView(view: SKView)
{
self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
self.physicsBody?.categoryBitMask = 1
self.physicsBody?.contactTestBitMask = 1
self.physicsWorld.gravity = CGVectorMake(0, -10)
self.physicsWorld.contactDelegate = self
}
func didBeginContact(contact: SKPhysicsContact) {
print("contact")
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?)
{
let spriteTexture = SKTexture(imageNamed: "Spaceship")
sprite = SKSpriteNode(texture: spriteTexture)
sprite.position = CGPoint(x: CGRectGetMidX(self.frame) , y: CGRectGetMidY(self.frame))
sprite.size = CGSizeMake(80, 80)
sprite.physicsBody = SKPhysicsBody(texture: spriteTexture, size: CGSizeMake(80, 80))
sprite.physicsBody?.categoryBitMask = 1
sprite.physicsBody?.collisionBitMask = 1
sprite.physicsBody?.contactTestBitMask = 1
sprite.physicsBody?.linearDamping = 0;
self.addChild(sprite)
}
}
if you paste this code and run you see a lot of "contact" string. I want only 1 contact this:
so i want when contact only 1 time collision
I edited my question can anyone help?
1.Create ‘Base Game’ from Xcode template based on SpriteKit
2.Paste code below to GameScene class
class GameScene: SKScene, SKPhysicsContactDelegate {
var sprite = SKSpriteNode()
override func didMoveToView(view: SKView)
{
self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
self.physicsBody?.categoryBitMask = 1
self.physicsBody?.collisionBitMask = 1
self.physicsWorld.gravity = CGVectorMake(0, -10)
self.physicsWorld.contactDelegate = self
self.physicsBody?.restitution = 0.0
}
func didEndContact(contact: SKPhysicsContact) {
print("End contact")
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?)
{
let spriteTexture = SKTexture(imageNamed: "Spaceship")
sprite = SKSpriteNode(texture: spriteTexture)
sprite.physicsBody = SKPhysicsBody(texture: spriteTexture, size: CGSizeMake(80, 80))
sprite.size = CGSizeMake(80, 80)
sprite.position = CGPoint(x: CGRectGetMidX(self.frame) , y: CGRectGetMidY(self.frame))
sprite.physicsBody?.categoryBitMask = 1
sprite.physicsBody?.collisionBitMask = 1
sprite.physicsBody?.contactTestBitMask = 1
sprite.physicsBody?.fieldBitMask = 1
sprite.physicsBody?.restitution = 0
self.addChild(sprite)
}
}
IMPORTANT NOTE Try to use didEndContact delegate method instead of didBeginContact. In this case you'll get only one invoke while didBeginContact invokes several times.

How do I allow MULTIPLE images to be moved using touch in SpriteKit?

so I am making this game in Swift and I have included the code of what I have done on the 'GameScene.swift' file.
I have 4 different objects that will need to be recreated at run time after every 0.2 seconds (don't even know how to do that :/), however using the code I currently have, if I try to move the 'coin20', 'coin100', or 'coin50' image, they don't move, only 'coin10' gets moved to their location and is far too sensitive so it just moves around like crazy whilst the other images are static.
I hope this question is specific enough since my past questions weren't well received by answerers, if not please let me know how to improve, i'm just a beginner in programming.
Thank you
Code:
import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate {
var coin10 = SKSpriteNode(imageNamed: "10S.png")
var coin100 = SKSpriteNode(imageNamed: "100S.png")
var coin50 = SKSpriteNode(imageNamed: "50S.png")
var coin20 = SKSpriteNode(imageNamed: "20S.png")
var wall1 = SKSpriteNode(imageNamed: "Wall1.png")
var wall2 = SKSpriteNode(imageNamed: "Wall2.png")
var bar = SKSpriteNode(imageNamed: "Bar.png")
var scorelabel = SKLabelNode()
var score = 0
var touchPoint: CGPoint = CGPoint()
var touching: Bool = false
enum ColliderType:UInt32 {
case coin = 1
case wall = 2
}
override func didMoveToView(view: SKView) {
/* Setup your scene here */
//Adding coin10
coin10.position = CGPointMake(self.size.width / 2, self.size.height / 5)
coin10.physicsBody = SKPhysicsBody(circleOfRadius: coin10.size.width/2)
coin10.physicsBody!.affectedByGravity = false
coin10.physicsBody!.categoryBitMask = ColliderType.coin.rawValue
coin10.physicsBody!.contactTestBitMask = ColliderType.wall.rawValue
coin10.physicsBody!.collisionBitMask = ColliderType.wall.rawValue
coin10.physicsBody!.dynamic = true
self.addChild(coin10)
//Adding coin100
coin100.position = CGPointMake(self.size.width / 1.7, self.size.height / 5.1)
coin100.physicsBody = SKPhysicsBody(circleOfRadius: coin100.size.width/2)
coin100.physicsBody!.affectedByGravity = false
coin100.physicsBody!.categoryBitMask = ColliderType.coin.rawValue
coin100.physicsBody!.contactTestBitMask = ColliderType.wall.rawValue
coin100.physicsBody!.collisionBitMask = ColliderType.wall.rawValue
coin100.physicsBody!.dynamic = true
self.addChild(coin100)
//Adding coin50
coin50.position = CGPointMake(self.size.width / 2.2, self.size.height / 4.9)
coin50.physicsBody = SKPhysicsBody(circleOfRadius: coin50.size.width/2)
coin50.physicsBody!.affectedByGravity = false
coin50.physicsBody!.categoryBitMask = ColliderType.coin.rawValue
coin50.physicsBody!.contactTestBitMask = ColliderType.wall.rawValue
coin50.physicsBody!.collisionBitMask = ColliderType.wall.rawValue
coin50.physicsBody!.dynamic = true
self.addChild(coin50)
//Adding coin20
coin20.position = CGPointMake(self.size.width / 2.4, self.size.height / 5)
coin20.physicsBody = SKPhysicsBody(circleOfRadius: coin20.size.width/2)
coin20.physicsBody!.affectedByGravity = false
coin20.physicsBody!.categoryBitMask = ColliderType.coin.rawValue
coin20.physicsBody!.contactTestBitMask = ColliderType.wall.rawValue
coin20.physicsBody!.collisionBitMask = ColliderType.wall.rawValue
coin50.physicsBody!.dynamic = true
self.addChild(coin20)
//Adding wall1
wall1.position = CGPointMake(self.size.width / 1.72, self.size.height / 1.07)
wall1.physicsBody = SKPhysicsBody(rectangleOfSize: wall1.frame.size )
wall1.physicsBody!.affectedByGravity = false
wall1.physicsBody!.categoryBitMask = ColliderType.wall.rawValue
wall1.physicsBody!.contactTestBitMask = ColliderType.coin.rawValue
wall1.physicsBody!.collisionBitMask = ColliderType.coin.rawValue
wall1.physicsBody!.dynamic = false
self.addChild(wall1)
//Adding wall2
wall2.position = CGPointMake(self.size.width / 2.5, self.size.height / 1.07)
wall2.physicsBody = SKPhysicsBody(rectangleOfSize: wall2.frame.size )
wall2.physicsBody!.affectedByGravity = false
wall2.physicsBody!.categoryBitMask = ColliderType.wall.rawValue
wall2.physicsBody!.contactTestBitMask = ColliderType.coin.rawValue
wall2.physicsBody!.collisionBitMask = ColliderType.coin.rawValue
wall2.physicsBody!.dynamic = false
self.addChild(wall2)
//Adding bar
bar.position = CGPointMake(self.size.width / 2, self.size.height / 1)
bar.physicsBody = SKPhysicsBody(rectangleOfSize: wall2.frame.size)
bar.physicsBody!.affectedByGravity = false
bar.physicsBody!.categoryBitMask = ColliderType.wall.rawValue
bar.physicsBody!.contactTestBitMask = ColliderType.coin.rawValue
bar.physicsBody!.collisionBitMask = ColliderType.coin.rawValue
bar.physicsBody!.dynamic = false
self.addChild(bar)
//Adding physics world properties
self.physicsWorld.contactDelegate = self
var scenebody = SKPhysicsBody(edgeLoopFromRect: self.frame)
scenebody.friction = 0
self.physicsBody = scenebody
self.physicsWorld.gravity = CGVectorMake(0, -0.3)
//Scoreboard
scorelabel = SKLabelNode(text: "0")
scorelabel.position.y = (self.size.height/2)
scorelabel.position.x = (self.size.height/2.3)
addChild(scorelabel)
}
func didBeginContact(contact: SKPhysicsContact) {
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
/* Called when a touch begins */
let touch = touches.first as! UITouch
for touch in (touches as! Set<UITouch>) {
let location = touch.locationInNode(self)
if coin10.containsPoint(location){
touchPoint = location
touching = true
}
else if coin100.containsPoint(location){
touchPoint = location
touching = true
}
else if coin20.containsPoint(location){
touchPoint = location
touching = true
}
else if coin50.containsPoint(location){
touchPoint = location
touching = true
}
}
}
override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
let touch = touches.first as! UITouch
for touch in (touches as! Set<UITouch>) {
let location = touch.locationInNode(self)
if coin10.containsPoint(location){
touchPoint = location
}
else if coin100.containsPoint(location){
touchPoint = location
}
else if coin50.containsPoint(location){
touchPoint = location
}
else if coin20.containsPoint(location){
touchPoint = location
}
}
}
override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
touching = false
}
override func update(currentTime: CFTimeInterval) {
if touching {
let dt: CGFloat = 1.0/60.0
let distance = CGVector(dx: touchPoint.x-coin10.position.x, dy: touchPoint.y-coin10.position.y)
let velocity = CGVector(dx: distance.dx/dt, dy: distance.dy/dt)
coin10.physicsBody!.velocity = velocity
}
}
}
First, add this instance variable
var touchedCoin:SKSpriteNode?
Then, in touchesBegan, set touchedCoin to the coin that was touched:
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
for touch in (touches as! Set<UITouch>) {
let location = touch.locationInNode(self)
if coin10.containsPoint(location) {
touchPoint = location
touching = true
touchedCoin = coin10
}
else if coin100.containsPoint(location){
touchPoint = location
touching = true
touchedCoin = coin100
}
else if coin20.containsPoint(location){
touchPoint = location
touching = true
touchedCoin = coin20
}
else if coin50.containsPoint(location){
touchPoint = location
touching = true
touchedCoin = coin50
}
}
}
Lastly, move the coin that was touched
override func update(currentTime: CFTimeInterval) {
if touching {
let dt: CGFloat = 1.0/60.0
let distance = CGVector(dx: touchPoint.x-touchedCoin!.position.x, dy: touchPoint.y-touchedCoin!.position.y)
let velocity = CGVector(dx: distance.dx/dt, dy: distance.dy/dt)
touchedCoin!.physicsBody!.velocity = velocity
}
}

Implementing collision detections

Basically the game consists of a basket that the player moves across the screen, the aim of the game is for the player to catch balls falling from the top of the screen. I am currently trying to add collision detection between the balls and the basket, but am facing difficulties namely, implementing this collision detection. I am new to swift, sprite kit and app development, so please help. Any help would be appreciated. Another problem I am facing is that all the balls are falling in the centre of the screen. A line of code is supposed to execute when, the ball hits the basket and following that the ball should disappear, please help as I am new to Spritekit.
import SpriteKit
class GameScene: SKScene {
var basket = SKSpriteNode()
let actionMoveRight = SKAction.moveByX(50, y: 0, duration: 0.2)
let actionMoveLeft = SKAction.moveByX(-50, y: 0, duration: 0.2)
//let physicsBody = SKPhysicsBody(texture: , size: 3500)
override func didMoveToView(view: SKView) {
/* Setup your scene here */
self.physicsWorld.gravity = CGVectorMake(0.0, -0.5)
self.backgroundColor = SKColor.whiteColor()
basket = SKSpriteNode(imageNamed: "basket")
basket.setScale(0.5)
basket.position = CGPointMake(self.size.width/2, self.size.height/8)
basket.size.height = 50
basket.size.width = 75
self.addChild(basket)
let updateAction = SKAction.runBlock {
var choice = arc4random_uniform(3)
switch choice {
case 1 :
var ball1 = SKSpriteNode(imageNamed: "redBall")
ball1.position = CGPointMake(self.size.width/3, self.size.height)
ball1.setScale(0.5)
ball1.size.height = 20
ball1.size.width = 30
ball1.physicsBody = SKPhysicsBody(circleOfRadius: ball1.size.height / 2.75)
ball1.physicsBody!.dynamic = true
self.addChild(ball1)
println("0")
case 0 :
var ball2 = SKSpriteNode(imageNamed: "redBall")
ball2.position = CGPointMake(self.size.width/5, self.size.height)
ball2.setScale(0.5)
ball2.size.height = 20
ball2.size.width = 30
ball2.physicsBody = SKPhysicsBody(circleOfRadius: ball2.size.height / 2.75)
ball2.physicsBody!.dynamic = true
self.addChild(ball2)
println("1")
case 2 :
var ball3 = SKSpriteNode(imageNamed: "redBall")
ball3.position = CGPointMake(self.size.width*4/5, self.size.height)
ball3.setScale(0.5)
ball3.size.height = 20
ball3.size.width = 30
ball3.physicsBody = SKPhysicsBody(circleOfRadius: ball3.size.height / 2.75)
ball3.physicsBody!.dynamic = true
self.addChild(ball3)
println("2")
default :
println("Problem")
}
}
let waitDuration : NSTimeInterval = 1.0
let updateAndWaitAction = SKAction.sequence([updateAction,SKAction.waitForDuration(waitDuration)])
let repeatForeverAction = SKAction.repeatActionForever(updateAndWaitAction)
self.runAction(repeatForeverAction)
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
/* Called when a touch begins */
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
if location.x > basket.position.x {
if basket.position.x < self.frame.maxX {
basket.runAction(actionMoveRight)
}
}
else {
if basket.position.x > self.frame.minX {
basket.runAction(actionMoveLeft)
}
}
}
}
override func update(currentTime: CFTimeInterval) {
}
}
For now you have a code that typically used in situations where user is taping something. You need to use BodyA & BodyB and assign a bitmasks to your nodes.
self.basket.physicsBody?.categoryBitMask = ColliderType.basket.rawValue
self.basket.physicsBody?.contactTestBitMask = ColliderType.ball1.rawValue
self.basket.physicsBody?.collisionBitMask = ColliderType.ball1.rawValue
self.basket.physicsBody?.contactTestBitMask = ColliderType.ball2.rawValue
self.basket.physicsBody?.collisionBitMask = ColliderType.ball2.rawValue
self.basket.physicsBody?.contactTestBitMask = ColliderType.ball3.rawValue
self.basket.physicsBody?.collisionBitMask = ColliderType.ball3.rawValue
And do that for every ball too. And then in func didBeginContact you should say to Xcode what to do, if you have an animation or something:
if (contact.bodyA.categoryBitMask == ColliderType.ball1.rawValue || contact.bodyB.categoryBitMask == ColliderType.ball1.rawValue) {
yourGameOverFunc()
}
if (contact.bodyA.categoryBitMask == ColliderType.ball2.rawValue || contact.bodyB.categoryBitMask == ColliderType.ball2.rawValue) {
yourGameOverFunc()
}
if (contact.bodyA.categoryBitMask == ColliderType.ball3.rawValue || contact.bodyB.categoryBitMask == ColliderType.ball3.rawValue) {
yourGameOverFunc()
}

didBeginContact not being called

I'm trying to create a program that prints out something whoever my spaceship goes over a circle, but it's not printing anything when I put the spaceship over the circle. Did I build my didBeginContact method wrong? Did I set up the BitMasks wrong?
import SpriteKit
class GameScene: SKScene {
var spaceship: SKNode!
var circ: SKNode!
override func didMoveToView(view: SKView) {
self.physicsWorld.gravity = CGVector(dx: 0, dy: 0)
spaceship = SKSpriteNode(imageNamed: "Spaceship")
spaceship.setScale(0.4)
spaceship.position.x = self.frame.width/2
spaceship.position.y = spaceship.frame.height/2
spaceship.physicsBody = SKPhysicsBody(circleOfRadius: spaceship.frame.height/2)
spaceship.physicsBody?.categoryBitMask = 1
spaceship.physicsBody?.contactTestBitMask = 2
spaceship.physicsBody?.collisionBitMask = 0
spaceship.physicsBody?.dynamic = true
circ = SKShapeNode(circleOfRadius: 50)
circ.position.y = self.frame.height/2
circ.position.x = self.frame.width/2
circ.physicsBody = SKPhysicsBody(circleOfRadius: 50)
circ.physicsBody?.categoryBitMask = 2
circ.physicsBody?.contactTestBitMask = 1
circ.physicsBody?.collisionBitMask = 0
circ.physicsBody?.dynamic = true
self.addChild(circ)
self.addChild(spaceship)
}
func didBeginContact(contact: SKPhysicsContact){
println("colliding!")
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
spaceship.position = location
}
}
override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
spaceship.position = location
}
}
}
You need to declare yourself as the contact delegate of your physics world:
// add conformance to SKPhysicsContactDelegate:
class GameScene: SKScene, SKPhysicsContactDelegate {
// ...
override func didMoveToView(view: SKView) {
self.physicsWorld.gravity = CGVector(dx: 0, dy: 0)
// set as delegate:
self.physicsWorld.contactDelegate = self
// ..
}
// should be called now
func didBeginContact(contact: SKPhysicsContact){
println("colliding!")
}
}

Resources