除一个碰撞外,所有碰撞都起作用.我该怎么办



我在场景中间有一个由四种不同颜色组成的正方形。同时,我从场景的每一侧随机生成了相同颜色的较小正方形,目的是与在中间的正方形碰撞。(蓝色变为蓝色、黄色变为黄色等)。

话虽如此,我的四次碰撞中有三次运行良好。红色打红色很好,蓝色打蓝色很好,黄色打黄色很好,但我的绿色方块不想做它应该做的。这就是我设置碰撞的方式:

  struct PhysicsCatagory {
     static let FirstPerson : UInt32 = 0x1 << 1
     static let SecondPerson : UInt32 = 0x1 << 2
     static let ThirdPerson : UInt32 = 0x1 << 4
     static let FourthPerson : UInt32 = 0x1 << 8
  }
  smallBlue.physicsBody?.categoryBitMask = PhysicsCatagory.FirstPerson
  smallBlue.physicsBody?.contactTestBitMask = PhysicsCatagory.SecondPerson
  smallRed.physicsBody?.categoryBitMask = PhysicsCatagory.ThirdPerson
  smallRed.physicsBody?.contactTestBitMask = PhysicsCatagory.FourthPerson
  smallRed.physicsBody?.collisionBitMask = 0
  smallGreen.physicsBody?.categoryBitMask = PhysicsCatagory.ThirdPerson
  smallGreen.physicsBody?.contactTestBitMask = PhysicsCatagory.FourthPerson
  smallGreen.physicsBody?.collisionBitMask = 0
  bigRed.physicsBody?.contactTestBitMask = PhysicsCatagory.FourthPerson
  bigRed.physicsBody?.categoryBitMask = PhysicsCatagory.ThirdPerson
  bigRed.physicsBody?.collisionBitMask = 0
  bigGreen.physicsBody?.categoryBitMask = PhysicsCatagory.ThirdPerson
  bigGreen.physicsBody?.contactTestBitMask = PhysicsCatagory.FourthPerson
  bigGreen.physicsBody?.collisionBitMask = 0
  bigBlue.physicsBody?.categoryBitMask = PhysicsCatagory.FirstPerson
  bigBlue.physicsBody?.contactTestBitMask = PhysicsCatagory.SecondPerson
  bigBlue.physicsBody?.collisionBitMask = 0
  bigYellow.physicsBody?.categoryBitMask = PhysicsCatagory.FirstPerson
  bigYellow.physicsBody?.collisionBitMask = 0
  bigYellow.physicsBody?.contactTestBitMask = PhysicsCatagory.SecondPerson
  func didBeginContact() {
      let firstBody = contact.bodyA.node as! SKSpriteNode // registering as big blue square
      let secondBody = contact.bodyB.node as! SKSpriteNode // register ing as little blue square
      if firstBody.color == secondBody.color { //if the colors collide, remove small one from the scene
        //firstBody.removeFromParent()
          label.text = "(numPoints)" // points label increment
          numPoints++ //points label increment 
          secondBody.removeFromParent()
      }
      if firstBody.color != secondBody.color { // if colors don't match, call gameOver scene
        //gameOver()
      }
      let thirdBody = contact.bodyA.node as! SKSpriteNode
      let fourthBody = contact.bodyB.node as! SKSpriteNode
      if thirdBody.color ==  fourthBody.color {           
        label.text = "(numPoints)"
        numPoints++
        fourthBody.removeFromParent()
      }
      if thirdBody.color != fourthBody.color {
        //gameOver()
      }
      let fifthBody = contact.bodyA.node as! SKSpriteNode
      let sixthBody = contact.bodyB.node as! SKSpriteNode
      if fifthBody.color ==  sixthBody.color {            
        label.text = "(numPoints)"
        numPoints++
        secondBody.removeFromParent()
      }
      let seventhBody = contact.bodyA.node as! SKSpriteNode
      let eighthBody = contact.bodyB.node as! SKSpriteNode
      if seventhBody.color == eighthBody.color {
          label.text = "(numPoints)"
          numPoints++
          eighthBody.removeFromParent()
          print("green removed")
      }        
  }

我有什么东西不见了吗?如有必要,将发布更多代码。

这是我要找的的英文代表

if small red equals big red then gain a point, update the label, and remove small red
if small blue equals big blue then gain a point, update the labelm and remove small blue
if small yellow equals big yellow then gain a point, update points label and remove little yellow.
if small green equals big green, gain point and update label and remove little yellow from scene
if little blue does not equal big blue, call game over scene
if little yellow does not equal big yellow, call game over scene
if little red does not equal big red, call game over scene
if little green does not equal big green, call game over scene

联系人主体的工作原理是,每次2个节点碰撞时都会调用didBeginContact,也就是说,在1个更新周期中,如果红色命中蓝色,红色命中绿色,则会对didBegginContact进行2次调用。这意味着你创造的第三个身体和第四个身体没有意义,因为它每次都和第一个身体一样。这意味着我们可以完全删除所有代码,并将其设置为:

func didBeginContact() {
    let firstBody = contact.bodyA.node as! SKSpriteNode // registering as big blue square
    let secondBody = contact.bodyB.node as! SKSpriteNode // register ing as little blue square
    if firstBody.color == secondBody.color { //if the colors collide, remove small one from the scene
        label.text = "(numPoints)" // points label increment
        numPoints++ //points label increment 
        secondBody.removeFromParent()
    }
    if firstBody.color != secondBody.color { // if colors don't match, call gameOver scene
    //gameOver()
    }

}

这段代码说的是,我不在乎这两个对象是什么,如果一个红色对象碰到一个红色的对象,让我们移除第二个对象。

好的,但现在我们有一个问题,如果第二个物体是红色的呢。

我们需要对代码进行编程,使第二个对象始终是小红色。这就是类别的由来。类别是这个对象应该是什么的整数表示。

如果我们使较大的对象类别遮罩低于较小的对象类别掩码,我们可以写出这样的公式:

let firstBody = ((contact.bodyA.categoryBitMask <= contact.bodyB.categoryBitMask) ? contact.bodyA.node : contactBodyB.node) as! SKSpriteNode // registering as big blue square
let secondBody = ((contact.bodyA.categoryBitMask <= contact.bodyB.categoryBitMask) ? contact.bodyB.node : contactBodyA.node) as! SKSpriteNode // registering as little blue square

它说如果A<=B或else bodyB如果A>B并且设secondBody是bodyB的节点如果A<=B或其他身体A,如果A>B。

按照你现在完成分类BitMask的方式,这是不可能的。您必须创建位掩码,这样较大的对象和较小的对象就不会共享同一个掩码。

现在我们保留了订单,所以我们的代码准确地说明了我们要寻找的内容。

如果你想添加一个删除颜色的打印,那么我们只需要在之后做一个条件来检查颜色。最终结果见下文。

func didBeginContact() {
    let firstBody = ((contact.bodyA.categoryBitMask <= contact.bodyB.categoryBitMask) ? contact.bodyA.node : contactBodyB.node) as! SKSpriteNode // registering as big blue square
    let secondBody = ((contact.bodyA.categoryBitMask <= contact.bodyB.categoryBitMask) ? contact.bodyB.node : contactBodyA.node) as! SKSpriteNode // registering as little blue square
    if firstBody.color == secondBody.color { //if the colors collide, remove small one from the scene
        label.text = "(numPoints)" // points label increment
        numPoints++ //points label increment 
        if(secondBody.color == UIColor.greenColor())
        {
           print("Green was removed")
        }
        if(secondBody.color == UIColor.redColor())
        {
           print("Red was removed")
        }
        if(secondBody.color == UIColor.blueColor())
        {
           print("Blue was removed")
        }
        if(secondBody.color == UIColor.yellowColor())
        {
           print("Yellow was removed")
        }

        secondBody.removeFromParent()
    }
    if firstBody.color != secondBody.color { // if colors don't match, call gameOver scene
    //gameOver()
    }


}

现在请记住,你永远不会去检查正在发生的碰撞类型,所以如果你决定在未来的配色中添加中等红色,这会导致问题。因为在它目前的状态下,我们只关心红色击中红色,我们不关心它是中等还是小。因此,在进行任何处理之前,您需要使用我们在另一个问题中谈到的切换方法,首先检查发生了什么类型的碰撞:

func didBeginContact() {
    let firstBody = ((contact.bodyA.categoryBitMask <= contact.bodyB.categoryBitMask) ? contact.bodyA.node : contactBodyB.node) as! SKSpriteNode // registering as big blue square
    let secondBody = ((contact.bodyA.categoryBitMask <= contact.bodyB.categoryBitMask) ? contact.bodyB.node : contactBodyA.node) as! SKSpriteNode // registering as little blue square

    switch(firstBody.categoryBitMask | secondBody.categoryBitMask)
    {
        case BigObject | SmallObject:
        if firstBody.color == secondBody.color { //if the colors collide, remove small one from the scene
            label.text = "(numPoints)" // points label increment
            numPoints++ //points label increment 
            if(secondBody.color == UIColor.greenColor())
            {
               print("Green was removed")
            }
            if(secondBody.color == UIColor.redColor())
            {
               print("Red was removed")
            }
            if(secondBody.color == UIColor.blueColor())
            {
               print("Blue was removed")
            }
            if(secondBody.color == UIColor.yellowColor())
            {
               print("Yellow was removed")
            } 

            secondBody.removeFromParent()
        }
        if firstBody.color != secondBody.color { // if colors don't match, call gameOver scene
        //gameOver()
        }
        default:()
    }


}

最新更新