2017-04-25 42 views
-5

看來我在這裏問了很多問題。我現在需要知道如何在物品被摧毀後讓物品掉落。我擁有它,所以當玩家與敵人相撞時,它會摧毀敵人,但是我需要它,所以當敵人被摧毀時,它會在其位置產生一個物品。spritekit敵人掉落物品

繼承人爲了消滅敵人代碼:

func handleAttackButtonHover(isHovering : Bool) { 
    if isHovering { 
     attackButton.texture = attackButtonPressedTexture 
     invincible = true 
     print("invincible = true") 
     playerNode?.removeAction(forKey:"animate") 
     playerNode?.run(attackAnimation,completion:{ 
      self.playerNode?.run(self.animation,withKey: "animate")}) 
    } else { 
     attackButton.texture = attackButtonTexture 
     invincible = false 
     print("invincible = false") 
     playerNode?.removeAction(forKey:"attackAnimation") 
     playerNode?.run(animation,completion:{ 
      self.playerNode?.run(self.animation,withKey: "animate")}) 
    } 
} 

也:

//physicsbody for player and enemies and coins 
func didBegin(_ contact: SKPhysicsContact) { 
    //  var bodyA = SKPhysicsBody() 
    //  var bodyB = SKPhysicsBody() 
    //print("collision detected") 
    if contact.bodyA.node?.name == "player" || contact.bodyB.node?.name == "player" { 
    //print("collision detected") 
     if contact.bodyA.node?.name == "enemy" { 
      if invincible == true { 
       contact.bodyA.node?.removeFromParent() 
      } else { 
       contact.bodyB.node?.removeFromParent() 
      } 
     } else if contact.bodyB.node?.name == "enemy" { 
      if invincible == true { 
       contact.bodyB.node?.removeFromParent() 
      } else { 
       contact.bodyA.node?.removeFromParent() 
      } 
     } 

    } 

} 
+0

那麼,**特定的**問題是什麼?爲什麼代碼轉儲? – dfd

+0

我在展示我如何摧毀敵人。我需要的是,我不知道什麼代碼將需要做到這一點,當敵人被摧毀,我會有一個項目在那裏產卵 –

+0

你有沒有嘗試過任何事情,使它發生或你只是複製粘貼代碼從你得到的答案? – BadgerBadger

回答

1

你可以做一個函數,在CGPoint。 CGPoint用於設置項目的位置。這樣

func spawnItem(point: CGPoint) { 
    //create the skspritenode 
    let item = SKSpriteNode(imageNamed: "coin"); 
    //Set the item position to the position you passed into the function 
    item.position = point 
    //Set the name of the item 
    item.name = "Coin" 

    //set up physics if needed 
    item.physicsBody = SKPhysicsBody.init(circleOfRadius: self.size.width/2) 
    item.physicsBody?.categoryBitMask = PhysicsCatagory.coin 
    item.physicsBody?.collisionBitMask = PhysicsCatagory.player 
    //And so on 

    //Then add it to your scene 
    self.addChild(item) 
} 

東西然後當你擊中敵人調用產卵函數傳入敵人的陣地。

func didBegin(_ contact: SKPhysicsContact) { 
    //  var bodyA = SKPhysicsBody() 
    //  var bodyB = SKPhysicsBody() 
    //print("collision detected") 
    if contact.bodyA.node?.name == "player" || contact.bodyB.node?.name == "player" { 
    //print("collision detected") 
     if contact.bodyA.node?.name == "enemy" { 
      if invincible == true { 
       spawnItem(point: contact.bodyA.node!.position) 
       contact.bodyA.node?.removeFromParent() 
      } else { 
       spawnItem(point: contact.bodyB.node!.position) 
       contact.bodyB.node?.removeFromParent() 
      } 
     } else if contact.bodyB.node?.name == "enemy" { 
      if invincible == true { 
       spawnItem(point: contact.bodyB.node!.position) 
       contact.bodyB.node?.removeFromParent() 
      } else { 
       spawnItem(point: contact.bodyA.node!.position) 
       contact.bodyA.node?.removeFromParent() 
      } 
     } 
    } 
} 

希望這能讓您走上正軌。

2

我會在創建時將物品附加到敵人身上,然後當它被摧毀時,可以將附加物品移動到場景中,然後殺死敵人。

func didBegin(_ contact: SKPhysicsContact) { 

    //You may want to think about doing integer base checks instead of string 
    let player = (contact.bodyA.node?.name == "player") ? contact.bodyA.node : (contact.bodyB.node?.name == "player") ? contact.bodyB.node : nil 
    let enemy = (contact.bodyA.node?.name == "enemy") ? contact.bodyA.node : (contact.bodyB.node?.name == "enemy") ? contact.bodyB.node : nil 

    if player != nil && enemy != nil{ 

     if invincible == true { 
      let item = player.childNodeWithName("item") 
      item.move(toParent:self) 
      item.isHidden = false 
      player.removeFromParent() 
     } else { 
      let item = enemy.childNodeWithName("item") 
      item.move(toParent:self) 
      item.isHidden = false 
      enemy.removeFromParent() 
     } 
    } 
}