didBeginContact没有被调用

我试图创build一个程序,可以打印出我的太空飞船绕过的任何东西,但是当我把飞船放在圆上的时候,它不会打印任何东西。 我build立了我的didBeginContact方法吗? 我是否设置了BitMasks?

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 } } } 

您需要声明自己是物理世界的联系人代表:

 // 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!") } }