迅速碰撞不起作用。我认为我在didBeginContact中的代码有问题

时间:2015-02-12 06:03:50

标签: swift collision bitmask

我一直在研究一个必须在我的测试节点与我的校验节点联系时执行测试的游戏。由于这两个节点是将在游戏中检查碰撞的唯一节点,我决定使用此方法来启动我的测试。

问题是当2个节点物理交叉时,我一直无法使碰撞工作。

当前代码将移动测试方块,直到它到达校验平方,但是现在他们见面时没有任何反应。

我错过了什么吗?任何帮助都会感激不尽。谢谢。

import SpriteKit

class GameScene: SKScene, SKPhysicsContactDelegate {


var test:SKSpriteNode!
var testTexture = SKTexture(imageNamed: "redsquare.png")
var check:SKSpriteNode!
var checkTexture = SKTexture(imageNamed: "bluesquare.png")
var moving:SKNode!
let testCategory: UInt32 = 1 << 0
let checkCategory: UInt32 = 1 << 2


override func didMoveToView(view: SKView) {
    /* Setup your scene here */

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

    moving = SKNode()
    self.addChild(moving)

    //create test block

    test = SKSpriteNode(texture: testTexture)
    test.xScale = 0.1
    test.yScale = 0.1
    test.position = CGPointMake(0, self.frame.height - test.size.height)

    test.physicsBody = SKPhysicsBody(rectangleOfSize: test.size)
    test.physicsBody?.dynamic = false

    test.physicsBody?.categoryBitMask = testCategory
    test.physicsBody?.collisionBitMask = checkCategory
    test.physicsBody?.contactTestBitMask = checkCategory

    self.addChild(test)

    //creates check block

    check = SKSpriteNode(texture: checkTexture)
    check.xScale = 0.1
    check.yScale = 0.1
    check.position = CGPointMake(self.frame.width, self.frame.height - check.size.height)

    check.physicsBody = SKPhysicsBody(rectangleOfSize: check.size)
    check.physicsBody?.dynamic = false

    check.physicsBody?.categoryBitMask = checkCategory
    check.physicsBody?.collisionBitMask = testCategory
    check.physicsBody?.contactTestBitMask = testCategory

    self.addChild(check)


    // move action

    let actionMove = SKAction.moveToX(self.frame.width, duration: 1.5)
    let actionMoveDone = SKAction.moveToX(0, duration: 0)

    let moveForever = SKAction.repeatActionForever(SKAction.sequence([actionMove,actionMoveDone]))
    test.runAction(moveForever)


}

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    /* Called when a touch begins */

    for touch: AnyObject in touches {

    }
}

override func update(currentTime: CFTimeInterval) {
    /* Called before each frame is rendered */
}

func didBeginContact(contact: SKPhysicsContact) {

// help here! 

    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 & testCategory) != 0 && (secondBody.categoryBitMask & checkCategory) != 0){


        println("contact!")

    }


}

}

1 个答案:

答案 0 :(得分:1)

根据Apple的文档,dynamic属性控制基于体积的体是否受到重力,摩擦,与其他对象的碰撞以及您直接应用于对象的力或冲动的影响。 / p>

  

动态属性:默认值为true。如果值为false,则物理体忽略   所有的力量和冲动都适用于它。此属性将被忽略   基于边缘的身体;它们是自动静止的。

因此,请将dynamic属性设置为true。

node.physicsBody?.dynamic = true

有关详细信息:https://developer.apple.com/library/prerelease/ios/documentation/SpriteKit/Reference/SKPhysicsBody_Ref/index.html#//apple_ref/occ/instp/SKPhysicsBody/