Ios swift - 使用categoryBitMask改变联系人

时间:2014-08-30 13:36:50

标签: ios iphone ipad swift

我的联系功能有问题。

我的游戏包括摧毁两种不同颜色的气球:蓝色和黄色

目前我的游戏效果很好,但我想在颜色之间做出区别,以计算我开发的蓝色或黄色气球的数量。

我初始化了尽可能多的categoryBitMask,因为我有颜色和我的projectil categoryBitMask:

let blueCategory:UInt32 = 0x1 << 1       //color category
let yellowCategory:UInt32 = 0x1 << 1

let balloonCategory:UInt32 = 0x1 << 1     // general balloons category

let flechetteCategory:UInt32 = 0x1 << 0   // dart category

气球定义:

func addBalloonbl(){


var balloonbl:SKSpriteNode = SKSpriteNode (imageNamed: "ballonbleu.png")


balloonbl.physicsBody = SKPhysicsBody (circleOfRadius: balloonbl.size.width/2)
balloonbl.physicsBody.dynamic = true
balloonbl.physicsBody.mass = 1
balloonbl.physicsBody.restitution = 1


balloonbl.physicsBody.categoryBitMask = balloonCategory | blueCategory
balloonbl.physicsBody.contactTestBitMask = flechetteCategory
balloonbl.physicsBody.collisionBitMask = balloonCategory





let minX = balloonbl.size.width/2
let maxX = self.frame.size.width - balloonbl.size.width/2
let rangeX = maxX - minX
let position:CGFloat = CGFloat(arc4random()) % CGFloat(rangeX) + CGFloat(minX)

balloonbl.position = CGPointMake(position, self.frame.size.height+balloonbl.size.height)

self.addChild(balloonbl)


}





func addBalloonj(){       



    var balloonj:SKSpriteNode = SKSpriteNode (imageNamed: "ballonjaune.png")

    balloonj.physicsBody = SKPhysicsBody (circleOfRadius: balloonj.size.width/2)
    balloonj.physicsBody.dynamic = true
    balloonj.physicsBody.mass = 1
    balloonj.physicsBody.restitution = 1

    balloonj.physicsBody.categoryBitMask = balloonCategory | yellowCategory
    balloonj.physicsBody.contactTestBitMask = flechetteCategory
    balloonj.physicsBody.collisionBitMask = balloonCategory


    let minX = balloonj.size.width/2
    let maxX = self.frame.size.width - balloonj.size.width/2
    let rangeX = maxX - minX
    let position:CGFloat = CGFloat(arc4random()) % CGFloat(rangeX) + CGFloat(minX)            
    balloonj.position = CGPointMake(position, self.frame.size.height+balloonj.size.height)

    self.addChild(balloonj)

}

我的联系功能:

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 & flechetteCategory) != 0 && (secondBody.categoryBitMask & blueCategory) != 0)
    {
       flecheDidCollideWithBlueBalloon(firstBody.node as SKSpriteNode, balloon: secondBody.node as SKSpriteNode)

    }

    else if ((firstBody.categoryBitMask & flechetteCategory) != 0 && (secondBody.categoryBitMask & yellowCategory) !=0)
    {
        flecheDidCollideWithYellowBalloon(firstBody.node as SKSpriteNode, balloon: secondBody.node as SKSpriteNode)

    }
}


func flecheDidCollideWithBlueBalloon (fleche: SKSpriteNode, balloon: SKSpriteNode)
    {

    self.runAction(SKAction.playSoundFileNamed("eclate.mp3", waitForCompletion: false))


    println("blue")


    fleche.removeFromParent()

    balloon.removeFromParent()



    balloonDestroyed++


    }


func flecheDidCollideWithBYellowBalloon (fleche: SKSpriteNode, balloon: SKSpriteNode)

   {     

    self.runAction(SKAction.playSoundFileNamed("eclate.mp3", waitForCompletion: false))


    println("Yellow")


    fleche.removeFromParent()       

    balloon.removeFromParent()


    balloonDestroyed++


  }

使用此代码,我可以销毁两种气球颜色但是当我销毁一个黄色的func flecheDidCollideWithBlueBalloon而不是flecheDidCollideWithYellowBalloon。 &#34;蓝色&#34;在控制台显示屏上发布...

有什么想法吗?

非常感谢!!

1 个答案:

答案 0 :(得分:0)

您的某些类别变量具有相同的值:

let blueCategory:UInt32 = 0x1 << 1       //color category
let yellowCategory:UInt32 = 0x1 << 1
let balloonCategory:UInt32 = 0x1 << 1     // general balloons category

由于您在同一个位掩码变量中组合了其中一些标志,因此它们之间存在冲突。

他们需要有不同的价值观:

let blueCategory:UInt32 = 0x1 << 1       //color category
let yellowCategory:UInt32 = 0x1 << 2
let balloonCategory:UInt32 = 0x1 << 3     // general balloons category