我怎样才能正确检测到接触和碰撞? Swift iOS SKScene

时间:2014-10-22 03:40:51

标签: ios swift sprite-kit ios7.1 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")
    }
}

谢谢你,希望你知道发生了什么,如果你需要任何额外的代码,请注释

2 个答案:

答案 0 :(得分:4)

您必须确保您使用的SKScene子类也实现SKPhysicsContactDelegate协议。例如,它看起来像这样。

class MyScene : SKScene, SKPhysicsContactDelegate

然后你必须将你的物理世界联系委托给你自己。

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

告诉我这是如何解决的,但它应该可以解决问题并成功让你听到碰撞。

答案 1 :(得分: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
}

}