如何在多次点击时删除节点

时间:2017-06-23 23:57:03

标签: swift sprite-kit

我正在制造一个太空入侵者游戏,其中许多敌舰向你射击,你必须射击它们。

当玩家触摸屏幕时,玩家船只向敌舰发射子弹。

我得到它,以便每当1个子弹接触敌舰时,它将从父母移除。但是我无法得到它以便从父母身上移除敌舰需要2发子弹。出于某种原因,当另一艘敌舰被召唤到现场时,敌人的生命将重置。我怎样才能使每艘敌舰拥有自己独立的生命并且不会影响其他敌舰的生命呢?

这是敌人阶级:

public class Villain: SKSpriteNode {

var life = 2

init(){

    let texture = SKTexture(imageNamed: "Villain")
    var life = 2
    print("number of lives: ", life)
    super.init(texture: texture, color: SKColor.clear, size: texture.size())
    self.name = "villain"
}

required public init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

这是名为Enemy类的GameScene类

func VillainRight(){
    let TooMuch = self.size.width
    let point = UInt32(TooMuch)

    let VillainR = Villain()

    VillainR.zPosition = 2
    VillainR.position = CGPoint(x: self.frame.minX,y: CGFloat(arc4random_uniform(point)))

    //This code makes the villain's Zposition point towards the SpaceShip
    let angle = atan2(SpaceShip.position.y - VillainR.position.y, SpaceShip.position.x - VillainR.position.x)
    VillainR.zRotation = angle - CGFloat(M_PI_2)

    let MoveToCenter = SKAction.move(to: CGPoint(x: self.frame.midX, y: self.frame.midY), duration: 15)

    //Physics World
    VillainR.physicsBody = SKPhysicsBody(rectangleOf: VillainR.size)
    VillainR.physicsBody?.categoryBitMask = NumberingPhysics.RightV
    VillainR.physicsBody?.contactTestBitMask = NumberingPhysics.Laser | NumberingPhysics.SpaceShip
    VillainR.physicsBody?.affectedByGravity = false
    VillainR.physicsBody?.isDynamic = true

    VillainR.run(MoveToCenter)
    addChild(VillainR)
}

,这是didBeginContact方法的一部分:

 //LASERS HIT ENEMY CHECK

    if BodyOne.categoryBitMask == NumberingPhysics.Laser && BodyTwo.categoryBitMask == NumberingPhysics.LeftV{
        run(VillainGone)
        ToNextLevel -= 1

        if BodyTwo.node != nil{
            MakeExplosions(BodyTwo.node!.position)
        }

        BodyTwo.node?.removeFromParent()
        BodyOne.node?.removeFromParent()
    }

    if BodyOne.categoryBitMask == NumberingPhysics.Laser && BodyTwo.categoryBitMask == NumberingPhysics.RightV{

        ToNextLevel -= 1

        if BodyTwo.node != nil{
            MakeExplosions(BodyTwo.node!.position)
        }

        run(VillainGone)
        BodyOne.node?.removeFromParent()
        BodyTwo.node?.removeFromParent()
    }
}

回顾:

我想做的就是一旦有2颗子弹触碰到敌人的船只就被移走了。并且敌人的生命彼此独立(如果一艘敌舰有1个生命,那么如果另一艘敌舰被召唤到现场,它将不会重置为2)。

1 个答案:

答案 0 :(得分:1)

以下是您的问题的示例解决方案(如果您想转换,这是一个macOS项目,只需将mouseDown替换为touchesBegan

点击屏幕观看恶棍健康消耗,当到达0时,恶棍将死亡并从场景中删除:

let category1 = UInt32(1)
let category2 = UInt32(2)


class Villain: SKSpriteNode {

  var lives = 2
  var hitThisFrame = false

  init(color: SKColor, size: CGSize) {
    super.init(texture: nil, color: color, size: size)
    let pb = SKPhysicsBody(rectangleOf: self.size)
    pb.categoryBitMask = category1
    pb.contactTestBitMask = category2
    self.physicsBody = pb
  }

  required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }
}

class Hero: SKSpriteNode {

  init(color: SKColor, size: CGSize) {
    super.init(texture: nil, color: color, size: size)
    let pb = SKPhysicsBody(rectangleOf: self.size)
    pb.categoryBitMask = category2
    pb.contactTestBitMask = category1
    self.physicsBody = pb
  }
  required init?(coder aDecoder: NSCoder) { fatalError() }
}


class GameScene: SKScene, SKPhysicsContactDelegate {

  let villain = Villain(color: .blue,  size: CGSize(width: 50, height: 50))
  let hero    = Hero   (color: .green, size: CGSize(width: 50, height: 50))

  override func didMove(to view: SKView) {
    physicsWorld.contactDelegate = self
    physicsWorld.gravity = CGVector.zero

    hero.position.y -= 100
    addChild(villain)
    addChild(hero)
  }

  func didBegin(_ contact: SKPhysicsContact) {
    let contactedBodies = contact.bodyA.categoryBitMask + contact.bodyB.categoryBitMask

    if contactedBodies == category1 + category2 {

      // Find which one of our contacted nodes is a villain:
      var vil: Villain
      if let foundVil = contact.bodyA.node as? Villain {
        vil = foundVil
      } else if let foundVil = contact.bodyB.node as? Villain {
        vil = foundVil
      } else {
        fatalError("one of the two nodes must be a villain!!")
      }


      if vil.hitThisFrame {
        // Ignore a second contact if already hit this frame:
        return
      } else {
        // Damage villain:
        vil.lives -= 1
        print(" vil lives: \(vil.lives)")
        vil.hitThisFrame = true
        if vil.lives == 0 {
          // Kill villain:
          print("villain is dead!!!")
          vil.physicsBody = nil
          vil.removeFromParent()
        }
      }
    }
  }

  override func didSimulatePhysics() {
    // Reset hero position (so as to not trigger another didBegin()
    hero.position = CGPoint(x: 0, y: -100)
    // Allow villain to be hit again next frame:
    villain.hitThisFrame = false
  }
  override func mouseDown(with event: NSEvent) {
    // Trigger didBegin():
    hero.position = villain.position
  }
}