didBeginContact没有被调用

时间:2014-07-29 01:45:25

标签: ios swift sprite-kit skspritenode skphysicsbody

我有2个需要检测其联系人的SKSpriteNodes。我已经尝试了各种方法并查找了很多东西,但似乎无法得到答案。以下是我的代码。 controlCircle是一个类级变量,因为它需要在其他方法中使用。生成的对象很好。

class GameScene: SKScene, SKPhysicsContactDelegate {

var controlCircle = SKSpriteNode()
var mainCategory : UInt32 = 1 << 0
var dropCategory: UInt32 = 1 << 1



override func didMoveToView(view: SKView) {

    self.physicsWorld.contactDelegate = self
    self.physicsWorld.gravity = CGVectorMake(0,0)

    //CREATING FIRST OBJECT AND ADDING PHYSICS BODY
    var mainRadius = 20.0;
    var controlCircle = SKSpriteNode(color: UIColor.clearColor(), size:CGSizeMake(mainRadius    * 2, mainRadius * 2))

    var circleBody = SKPhysicsBody(circleOfRadius: mainRadius)
    circleBody.dynamic = false
    circleBody.usesPreciseCollisionDetection = true

    controlCircle.physicsBody = circleBody

    var bodyPath = CGPathCreateWithEllipseInRect(CGRectMake((controlCircle.size.width/2),   controlCircle.size.height/2, controlCircle.size.width, controlCircle.size.width),
        nil)

    var circleShape = SKShapeNode()
    circleShape.fillColor = UIColor.brownColor()
    circleShape.lineWidth = 0
    circleShape.path = bodyPath
    controlCircle.addChild(circleShape)
    controlCircle.position = CGPointMake(self.frame.width/2, self.frame.height/2)

    self.addChild(controlCircle)
    controlCircle.physicsBody.categoryBitMask = mainCategory
    controlCircle.physicsBody.contactTestBitMask = dropCategory


    //CREATING SECOND OBJECT AND ADDING PHYSICS BODY 
    var radius = 10.0;
    var drop = SKSpriteNode(color: UIColor.clearColor(), size:CGSizeMake(radius * 2, radius * 2))

    var dropBody = SKPhysicsBody(circleOfRadius: radius)
    dropBody.dynamic = false
    dropBody.usesPreciseCollisionDetection = true

    drop.physicsBody = dropBody

    var dropPath = CGPathCreateWithEllipseInRect(CGRectMake((drop.size.width/2), drop.size.height/2, drop.size.width, drop.size.width),
        nil)

    var dropShape = SKShapeNode()
    dropShape.fillColor = UIColor.blackColor()
    dropShape.lineWidth = 0

    drop.name = "dropMask"
    dropShape.path = dropPath


    drop.addChild(dropShape)
    drop.position = CGPointMake(CGFloat(xValue), self.frame.height-5)

    self.addChild(drop)
    drop.physicsBody.categoryBitMask = dropCategory
    drop.physicsBody.contactTestBitMask = mainCategory
}

    func didBeginContact(contact: SKPhysicsContact) -> Void{
    NSLog("Hello")

    }
}

1 个答案:

答案 0 :(得分:2)

大家好消息,

SKPhysicsBody文档:

动态属性控制基于体积的主体是否受重力,摩擦,与其他对象的碰撞以及您直接应用于对象的力或冲动的影响。

所以这里有一个想法,即在不拖动但是接收碰撞事件时完成静止物体。触摸SKSpriteNode上的touchesBegan,在touchUp physicsBody.dynamic = true上设置其physicsBody.dynamic = false

或者,您可以从系统中删除重力,并使所有节点都具有physicsBody.dynamic = true