spritekit敌人丢弃一个项目

时间:2017-04-25 23:13:40

标签: swift sprite-kit

似乎我在这里问过很多问题。我现在需要知道如何让敌人在物品被摧毁后丢弃物品。我有它,所以当玩家与敌人发生碰撞时,它会摧毁敌人,但我需要它,所以当敌人被摧毁时,它会在其位置产生一个物品。

下面是摧毁敌人的代码:

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

    }

}

2 个答案:

答案 0 :(得分: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()
         }
    }
}

答案 1 :(得分: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()
            }
        }
    }
}

希望这能让你走上正轨。