2017-06-23 5 views
1

多くの敵艦があなたの方に来て、あなたがそれらに撃たなければならない宇宙侵略ゲームを作っています。複数回ヒットしたときにノードを削除する方法

プレイヤーが画面に触れると、プレイヤー船は敵の船で弾丸を撃つ。

1個の弾丸が敵の船に触れるたびに親から取り除かれるようにしました。しかし、私はそれを得ることができないように2つの弾丸が敵の船を親から削除する必要があります。なんらかの理由で、もう1つの敵船が場に呼び出されてから、敵の命が復活する。どのようにして各敵の船が独自の生活を持ち、他の敵の船の生活に影響を与えないようにすることができますか?ここで

は敵のクラスである:ここで
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") 
} 

は敵のクラスは

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) 
} 

と呼ばれているGameSceneクラスであり、ここで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() 
    } 
} 

RECAP:

私がしたいことは、弾丸が2回触れると、敵の船が取り外されてしまうことです。そして、敵の生活は互いに独立しています(敵の船のライフが1つ残っていれば、もう1つの敵の船がシーンに呼び出されると2にリセットされません)。

+0

私は頻繁に提案すると、一緒にそののuserDataを通じて各ノードに一意のIDを与えますヒット数。 –

+0

ノードを複数回追加する関数を呼び出すと、その方法を教えてください。 –

+0

あなたは検索を実行して自分自身で回答を見つけることを止めませんか? –

答えて

1

問題の解決策のサンプルを示します(変換する場合は、mouseDowntouchesBeganに置き換えてください)。

を悪役健康デプリートを見るために、画面をクリックし、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 
    } 
} 
+0

こんにちは、これは時々動作しています。致命的なエラーメッセージが表示されることがあります。「2つのノードのいずれかが悪人でなければならない!!」なぜこのメッセージがポップアップするのか教えていただけますか? –

+1

私のコードにバグがあります。今すぐ修正しましょう。これは修正する必要があります - 私は 'bodyB.NODE'を参照するのを忘れ、代わりに' bodyB'を持っていました。これは私が 'fatalError()'を愛している理由です。あなたが間違いを犯した時にそれを伝えるからです:) @AlexMerlin – Fluidity

+0

素晴らしいです!私が長い間持っていたこの問題を解決するのを手伝ってくれてありがとう:) @Fluidity !! –

-1

ノードを削除する場合は、removeAllChildren()を試してください。< - それをコピーして貼り付けます。

+1

それは彼が求めている質問ではない。 –

+0

これはまた、ヒーロー/プレイヤーノードも削除し、ゲームを台無しにするでしょう:P感謝します。^ - ^回答を投稿し続けると、あなたの知識と担当者が改善されます! – Fluidity

関連する問題