SpritKit swift 3物理体碰撞并只删除一个节点

时间:2016-09-24 12:20:40

标签: ios swift sprite-kit skspritenode

我在swift 3上使用Sprit-kit我的问题是在向一个节点射击子弹后(我称之为Planet)我不希望该节点(Planet)消失我想只有子弹从场景中消失并且我的节点(Planet)仍然继续运行,如果那个节点(Planet)与主播放器发生冲突,它们都会消失(这里没有问题)。我的问题是当子弹与那个节点发生碰撞时,它们都消失了在碰撞功能我刚刚写了Bullet.removefromParent()这里是我的代码(我希望保持我的星球与子弹碰撞后我的子弹从场景中删除):

 func CollitionPlannetWithBullet(_ Bullet : SKSpriteNode, Planet : SKSpriteNode){
   Bullet.removeFromParent()
 }

这是行星和子弹的开始接触功能的代码:

 if ((firstBody.categoryBitMask == physicsCatagory.Planet) && 
     (secondBody.categoryBitMask == physicsCatagory.Bullet)) ||
     ((firstBody.categoryBitMask == physicsCatagory.Bullet) &&
     (secondBody.categoryBitMask == physicsCatagory.Planet))  {
       CollitionPlannetWithBullet(firstBody.node as! SKSpriteNode, Planet:   secondBody.node as! SKSpriteNode)
 }

1 个答案:

答案 0 :(得分:0)

我发现执行此操作的最佳方法是升级SKSpriteNode,然后允许您使用if来使用其一个子类函数处理碰撞中的每个节点。

我建议阅读子类和多态,因为它会使你的代码更清晰,并为你构建的东西提供更多的灵活性。

func evalChainGun_Drone(_ contact: SKPhysicsContact){
    let nodeA = contact.bodyA.node
    let nodeB = contact.bodyB.node

    //A Items
    if let shotA = nodeA as? ChaingunBullet {
        shotA.damage()}

    if let droneA = nodeA as? Drone {
        droneA.weaponDamage(droneA.HP_Class, damage: 1, node: droneA, contactLocation: contact.contactPoint)
    }

    //B Items
    if let shotB = nodeB as? ChaingunBullet {
        shotB.damage()}

    if let droneB = nodeB as? Drone {
        droneB.weaponDamage(droneB.HP_Class, damage: 1, node: droneB, contactLocation: contact.contactPoint)
    }
}

我希望这有用。

祝你好运, 亡灵地球

相关问题