2014-10-22 50 views
2

問題是我無法檢測以任何方式我已經在網上我如何正確檢測接觸和碰撞?斯威夫特的iOS SKScene

上發現的碰撞//或接觸這是我的代碼:

我的面具的聲明:

private let ballCategory : UInt32 = 0x1 << 0 
private let holeCategory : UInt32 = 0x1 << 1 

孔都和球的添加:

func addHole(#size : CGSize) { 

    let actionMoveDown = SKAction.moveToY(CGRectGetMidY(self.frame)-500, duration: 4.7) 
    let hole = shapedHoles() 
    let UT = UTIL() 
    var position:CGFloat 


    let randomPosition = UT.randomNumberWith(Min: 1, Max: 3) 

    switch randomPosition{ 
    case 1: 
     position = CGRectGetMidX(self.frame) 
    case 2: 
     position = CGRectGetMidX(self.frame)+size.width 

    default: 
     position = CGRectGetMidX(self.frame)-(size.width) 

    } 



    var createdHole = hole.createHoleAtPosition(position: CGPointMake(position ,CGRectGetMaxY(self.frame) + (size.height/2)),size: size)//CGSizeMake(CGRectGetMaxX(self.frame)/3 - 10, 70) 

    createdHole.physicsBody = SKPhysicsBody(rectangleOfSize: createdHole.frame.size) 
    createdHole.physicsBody?.categoryBitMask = holeCategory 
    createdHole.physicsBody?.collisionBitMask = 0 
    createdHole.physicsBody?.contactTestBitMask = ballCategory 
    createdHole.physicsBody?.affectedByGravity = false 
    createdHole.physicsBody?.dynamic = false 


    lastHolePosition = randomPosition 

    createdHole .runAction(actionMoveDown) 

    self.addChild(createdHole) 



} 

func addSphere(){ 
    let mainCharacterController = circle() 
    let character: (SKNode) = mainCharacterController.createCircleAtPosition(position: CGPointMake(CGRectGetMidX(self.frame), CGRectGetMinY(self.frame)+100)) 


    character.physicsBody = SKPhysicsBody(circleOfRadius: character.frame.size.height/2) 
    character.physicsBody?.categoryBitMask = ballCategory 
    character.physicsBody?.collisionBitMask = 0 
    character.physicsBody?.contactTestBitMask = holeCategory 
    character.physicsBody?.affectedByGravity = false 
    character.physicsBody?.dynamic = false 


    self.addChild(character) 


} func addHole(#size : CGSize) { 

    let actionMoveDown = SKAction.moveToY(CGRectGetMidY(self.frame)-500, duration: 4.7) 
    let hole = shapedHoles() 
    let UT = UTIL() 
    var position:CGFloat 


    let randomPosition = UT.randomNumberWith(Min: 1, Max: 3) 

    switch randomPosition{ 
    case 1: 
     position = CGRectGetMidX(self.frame) 
    case 2: 
     position = CGRectGetMidX(self.frame)+size.width 

    default: 
     position = CGRectGetMidX(self.frame)-(size.width) 

    } 



    var createdHole = hole.createHoleAtPosition(position: CGPointMake(position ,CGRectGetMaxY(self.frame) + (size.height/2)),size: size)//CGSizeMake(CGRectGetMaxX(self.frame)/3 - 10, 70) 

    createdHole.physicsBody = SKPhysicsBody(rectangleOfSize: createdHole.frame.size) 
    createdHole.physicsBody?.categoryBitMask = holeCategory 
    createdHole.physicsBody?.collisionBitMask = 0 
    createdHole.physicsBody?.contactTestBitMask = ballCategory 
    createdHole.physicsBody?.affectedByGravity = false 
    createdHole.physicsBody?.dynamic = false 


    lastHolePosition = randomPosition 

    createdHole .runAction(actionMoveDown) 

    self.addChild(createdHole) 



} 

func addSphere(){ 
    let mainCharacterController = circle() 
    let character: (SKNode) = mainCharacterController.createCircleAtPosition(position: CGPointMake(CGRectGetMidX(self.frame), CGRectGetMinY(self.frame)+100)) 


    character.physicsBody = SKPhysicsBody(circleOfRadius: character.frame.size.height/2) 
    character.physicsBody?.categoryBitMask = ballCategory 
    character.physicsBody?.collisionBitMask = 0 
    character.physicsBody?.contactTestBitMask = holeCategory 
    character.physicsBody?.affectedByGravity = false 
    character.physicsBody?.dynamic = false 


    self.addChild(character) 


} 

最後但並非最不重要的didBeginContactMethod

func didBeginContact(contact: SKPhysicsContact) { 

    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 & holeCategory) != 0 && 
     (secondBody.categoryBitMask & ballCategory) != 0 { 
     println("HO") 
    } 
} 

謝謝你,希望你知道發生了什麼,如果你需要任何額外的代碼只是評論它

+0

注意,與collisionBitMask = 0,你將只能收到接觸回調,但機構將通過彼此,即沒有碰撞響應 – LearnCocos2D 2014-10-22 09:27:36

回答

1
There is a couple things it might be. First you can try using an enum for the contact categories. 

enum collisionBodies:UInt32 { 
    case ballCategory = 1 
    case holeCategory = 2 
} 

func Collisions() { 
    character.physicsBody.categoryBitMask = collisionBodies.ballCategory.rawValue 
    character.physicsBody.contactTestBitMask = collisionBodies.holeCategory.rawValue 
    character.physicsBody.collisionBitMask = 0 

    createdHole.physicsBody.categoryBitMask = collisionBodies.holeCategory.rawValue 
    createdHole.physicsBody.contactTestBitMask = collisionBodies.ballCategory.rawValue 
    createdHole.physicsBody.collisionBitMask = 0 
} 

Also try setting one of your physics bodies to dynamic = true or collisions will likely not work. 

A better way to test for collisions in the didBeginContact function is to use a switch statement. 

func didBeginContact(contact: SKPhysicsContact) { 
    let categoryMask = contact.BodyA.categoryBitMask | contact.BodyB.categoryBitMask 

    switch (categoryMask) { 
    case collisionBodies.holeCategory.rawValue | collisionBodies.ballCategory.rawValue: 
     //code to run when contact is detected 
    default: 
    return 
} 

}

4

你必須確保你正在使用也SKScene子類實現SKPhysicsContactDelegate協議。例如,它看起來像這樣。

class MyScene : SKScene, SKPhysicsContactDelegate 

然後你必須設置你的物理世界聯繫委託給自己。

override init() { 
    self.physicsWorld.contactDelegate = self; 
} 

告訴我這是如何解決的,但它應該解決問題,併成功地讓您傾聽碰撞。