2014-10-30 49 views
1

我正在製作一個遊戲,我需要我的spriteNodes發生碰撞。我跟着一個教程,但它只是不適合我。didBeginContact不叫

當我運行我的遊戲和兩個對象發生碰撞時,輸出監視器中沒有println(「beginContact」),因此didBeginContact函數未被調用。

override init(size:CGSize) { 
    super.init(size: size) 
    player.planeSprite = SKSpriteNode(imageNamed: "plane5") 

    player.planeSprite.physicsBody? = SKPhysicsBody(rectangleOfSize: player.planeSprite.size) 
    player.planeSprite.physicsBody?.dynamic = true; 
    player.planeSprite.physicsBody?.categoryBitMask = playerCategory; 
    player.planeSprite.physicsBody?.contactTestBitMask = noteCategory; 
    player.planeSprite.physicsBody?.collisionBitMask = 0; 
    player.planeSprite.physicsBody?.usesPreciseCollisionDetection = true; 

    player.planeSprite.position = CGPointMake(player.legalPositions[1], player.planeSprite.size.height/2) 

    self.addChild(player.planeSprite) 
} 

func addNote() { 
    var note:SKSpriteNode = SKSpriteNode(imageNamed: "note") 
    note.physicsBody? = SKPhysicsBody(rectangleOfSize: note.size) 
    note.physicsBody?.dynamic = true 
    note.physicsBody?.categoryBitMask = noteCategory 
    note.physicsBody?.contactTestBitMask = playerCategory 
    note.physicsBody?.collisionBitMask = 0 

    let minX = note.size.width/2 
    let maxX = self.frame.size.width - note.size.width/2 
    let rangeX = maxX - minX 
    let position = Int(arc4random_uniform(3)) 

    note.position = CGPointMake(player.legalPositions[position], self.frame.size.height + note.size.height) 

    self.addChild(note) 
} 

func didBeginContact(contact: SKPhysicsContact!) { 
    println("beginContact") 

    var firstBody:SKPhysicsBody 
    var secondBody:SKPhysicsBody 

    if (contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask) { 
     firstBody = contact.bodyA 
     secondBody = contact.bodyB 
    } 
    else { 
     firstBody = contact.bodyB 
     secondBody = contact.bodyA 
    } 

    if((firstBody.categoryBitMask & noteCategory) != 0 && (secondBody.categoryBitMask & playerCategory) != 0) { 
     println("if statement") 
     playerDidCollideWithNote(firstBody.node as SKSpriteNode, note: secondBody.node as SKSpriteNode) 
    } 
} 

func playerDidCollideWithNote(plane:SKSpriteNode, note:SKSpriteNode) { 
    println("hit") 
    note.removeFromParent() 
} 
+2

通常不調用函數的問題是因爲未設置.delegate屬性。 – JMFR 2014-10-30 13:51:35

+0

是你需要設置場景作爲物理世界代表 – LearnCocos2D 2014-10-30 14:47:36

+0

你的意思是這樣的:class GameScene:SKScene,SKPhysicsContactDelegate { – Murturn 2014-10-30 14:52:00

回答

-2

從文檔,

實現時具有重疊 contactTestBitMask值的兩個物理機構是在一個物理 世界彼此接觸的SKPhysicsContactDelegate協議可以響應的對象。要接收聯繫人消息,請設置SKPhysicsWorld對象的contactDelegate 屬性。代表在 聯繫人開始或結束時被調用。

因此,你需要採取SKPhysicsContactDelegate協議和

class GameScene: SKScene, SKPhysicsContactDelegate { 
    override func didMove(to view: SKView) { 
     physicsWorld.contactDelegate = self 
    } 
} 

另外,physicsWorldcontactDelegate屬性設置爲SKScene,你應該刪除?從下面的陳述

player.planeSprite.physicsBody? = SKPhysicsBody(rectangleOfSize: player.planeSprite.size) 

note.physicsBody? = SKPhysicsBody(rectangleOfSize: note.size) 
11

做了幾次misstake。

首先,您需要從SKPhysicsContactDelegate繼承:

class GameScene: SKScene, SKPhysicsContactDelegate { ... } 

,然後在didMoveToView方法,添加:

override func didMoveToView(view: SKView) { 
    self.physicsWorld.contactDelegate = self 
} 

要設置GameScene實例作爲contactDelegate的physicsWorld。

+0

NICE ONE !! :) - 只是一個簡短的問題,你什麼時候會使用這個分配給另一個對象? – 2016-08-24 18:33:49

8

如果使用Xcode8.0 Swift3.0,您不能使用
func didBeginContact(contact: SKPhysicsContact!) {}

所以,你應該用這個。 func didBegin(_ contact: SKPhysicsContact) {}

+0

我的屁股疼痛,做到了!我從舊的swift轉換到3.0,並不能解決這個問題。謝謝! – bworby 2017-02-02 06:32:49

+0

這樣的救星!謝謝@Rasukaru – Marin 2017-11-26 09:12:01