如果多次触摸,如何从父节点中删除节点?

时间:2016-07-21 07:11:16

标签: swift sprite-kit swift2

我正在制作一个游戏,当按下按钮时子弹射向坏人。我做了一个函数,无论何时调用它都会增加更多坏人。

以下是代码:(此方法被多次调用)

 func BadGuyPosition()
    let BadGuyCircle = SKSpriteNode(imageNamed: "CircleBadGuy")
    BadGuyCircle.zPosition = 1
    //var mininmum = self.size.width / 600
    let TooMuch = self.size.height - 60

    let PointToShow = UInt32(TooMuch)

    BadGuyCircle.position = CGPointMake(CGRectGetMaxX(self.frame) + 20 , CGFloat(arc4random_uniform(PointToShow) ))

    let action2 = SKAction.moveToX(-100, duration: 5.0)
    let remove = SKAction.removeFromParent()

    BadGuyCircle.runAction(SKAction.sequence([action2,remove]))

    //Physics BadGuy
    BadGuyCircle.physicsBody = SKPhysicsBody(rectangleOfSize: BadGuyCircle.size)
    BadGuyCircle.physicsBody?.categoryBitMask = Numbering.Badguy
    BadGuyCircle.physicsBody?.contactTestBitMask = Numbering.Laser
    BadGuyCircle.physicsBody?.affectedByGravity = false
    BadGuyCircle.physicsBody?.dynamic = true

    self.addChild(BadGuyCircle)

我想要这样,如果与坏人接触了2颗子弹,就会从父母身上移除坏人。

我得到它,以便当1个子弹与敌人接触时,它会从父母身上移除。 (这是代码)

func didBeginContact(contact: SKPhysicsContact) {
    let A : SKPhysicsBody = contact.bodyA
    let B : SKPhysicsBody = contact.bodyB

    if (A.categoryBitMask == Numbering.Badguy) && (B.categoryBitMask == Numbering.Laser) || (A.categoryBitMask == Numbering.Laser) && (B.categoryBitMask == Numbering.Badguy)
    {

        runAction(BadGuyLostSound)
        bulletsTouchedBadGuy(A.node as! SKSpriteNode, Laser: B.node as! SKSpriteNode)

    }









}

func bulletsTouchedBadGuy(BadGuy: SKSpriteNode, Laser: SKSpriteNode){

    Laser.removeFromParent()
    BadGuy.removeFromParent()


}

任何人都可以告诉我,我怎样才能让敌人从父母身上移走不止一颗子弹。

2 个答案:

答案 0 :(得分:4)

删除碰撞节点的最佳方法是使用方法didFinishUpdate,如果您移除或启动一个方法从didBeginContact移除您的节点,您的游戏可能会崩溃搜索同时位于的碰撞节点被移除的过程..

class BadGuy: SKSpriteNode {
   var badGuyBulletCollisionsCounter: Int = 0
   init() {
       let texture = SKTexture(imageNamed: "CircleBadGuy")
       super.init(texture: texture, color: nil, size: texture.size())
       ...
       // fill this part with your BadGuy code
   }
   required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
   }  
}

声明全局var:

var nodesToRemove = [SKNode]()

didBeginContact方法中:

func didBeginContact(contact: SKPhysicsContact) {
    let A : SKPhysicsBody = contact.bodyA
    let B : SKPhysicsBody = contact.bodyB
    if (A.categoryBitMask == Numbering.Badguy) && (B.categoryBitMask == Numbering.Laser) || (A.categoryBitMask == Numbering.Laser) && (B.categoryBitMask == Numbering.Badguy)
    {
        badGuy = A.node as! BadGuy
        badGuy.badGuyBulletCollisionsCounter += 1
        runAction(BadGuyLostSound)
        bulletsTouchedBadGuy(badGuy, Laser: B.node as! SKSpriteNode)
    }
}

bulletsTouchedBadGuy方法中:

func bulletsTouchedBadGuy(badGuy: BadGuy, laser: SKSpriteNode){
    nodesToRemove.append(laser)
    if badGuy.badGuyBulletCollisionsCounter == 2 {
       nodesToRemove.append(badGuy)
    }   
}

最后:

override func didFinishUpdate() 
{   
     nodesToRemove.forEach(){$0.removeFromParent()}   
     nodesToRemove = [SKNode]()
}

答案 1 :(得分:0)

使用节点' userData跟踪它的命中状态。当子弹击中BadGuy时,检查节点' userData以查看它是否已被命中。如果有,请将其删除;如果没有,请表明它有:

//Add this when creating BadGuy:
BagGuy.userData = ["HasBeenHit" : false]

    func bulletsTouchedBadGuy(BadGuy: SKSpriteNode, Laser: SKSpriteNode){

        Laser.removeFromParent()

        if BadGuy.userData["HasBeenHit"] == true {
           BadGuy.removeFromParent()
        } else {
           BadGuy.userData["HasBeenHit"] = true
        }

甚至:

        if BadGuy.userData["HasBeenHit"] == true ? BadGuy.removeFromParent() : BadGuy.userData["HasBeenHit"] = true

如果您想将removeFromParent()移至didFinishUpdate(),只需按以下步骤更改操作:

        if BadGuy.userData["HasBeenHit"] == true {
           BadGuy.userData["HasBeenHitTwice"]
        } else {
           BadGuy.userData["HasBeenHit"] = true
        }

然后在didFinishUpdate()中删除具有此值集的所有节点,或者根据另一个答案构造要删除的节点数组。