Swift Collision Detection无法在spriteKit中运行

时间:2015-01-29 15:43:50

标签: ios swift collision detection

我正在尝试引入一个功能,当用户节点(birdimage)点击我的powerup图像时,用户当前得分增加10 - 我正在使用spriteKit

目前这是我的上电功能:

func plustenSpawn() {

    var tenPUText = SKTexture(imageNamed: "plusten")
    tenPU = SKSpriteNode(texture: tenPUText)
    tenPU.size.height = tenPU.size.height/13
    tenPU.size.width = tenPU.size.width/13
    tenPU.zPosition = 100
    tenPU.physicsBody = SKPhysicsBody(rectangleOfSize: tenPU.size)
    tenPU.physicsBody?.dynamic = true
    tenPU.physicsBody?.categoryBitMask = ten

    self.addChild(tenPU)

    let maxHeight:UInt32 = UInt32(self.frame.size.height - enemyBird.size.height)
    var randomHeight = CGFloat(arc4random() % maxHeight)
    if (randomHeight < self.frame.size.height*0.1) {
        randomHeight = self.frame.size.height*0.12
    }
    tenPU.position = CGPointMake(self.frame.size.width+100,randomHeight)

    let moveToEndAction = SKAction.moveToX(10, duration: 1.3)
    let removeFromView = SKAction.removeFromParent()

    let moveToEndThenStartAgain = SKAction.repeatActionForever(SKAction.sequence([moveToEndAction, removeFromView]))

    tenPU.runAction(SKAction.repeatActionForever(moveToEndThenStartAgain))


}

然后我的鸟就像这样设置

    func birdPhysics() {
    // Give bird physics
    bird.physicsBody = SKPhysicsBody(circleOfRadius: bird.size.height/3)
    bird.physicsBody?.mass = 0.2
    bird.physicsBody?.dynamic = true
    bird.physicsBody?.allowsRotation = false
    bird.physicsBody?.categoryBitMask = birdGroup
    bird.physicsBody?.collisionBitMask = objectGroup | ten
    bird.physicsBody?.contactTestBitMask = objectGroup | ten

}

出于某种原因,当我的鸟(birdGroup)击中我的通电(十)时,得分并没有上升 - 但是当我的鸟击中我的对象组时,得分确实上升了......我知道这有一个简单的解释但我无法理解它 - 我还在ViewDidLoad中设置了所有碰撞代理

这是我的碰撞检测:

func didBeginContact(contact: SKPhysicsContact) {

    if contact.bodyA.categoryBitMask == ten || contact.bodyB.categoryBitMask == ten {
        println("collision")
        score = score+10
        scoreLabel.text = "\(score)"
    }

0 个答案:

没有答案