【问题标题】:spritekit enemy dropping an itemspritekit 敌人掉落物品
【发布时间】:2017-04-25 23:13:39
【问题描述】:

看来我在这里问了很多问题。我现在需要知道如何让敌人在物品被摧毁后掉落它。我有它,所以当玩家与敌人碰撞时,它会摧毁敌人,但我需要它,所以当敌人被摧毁时,它会在它的位置产生一个物品。

这是消灭敌人的代码:

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

    }

}

【问题讨论】:

  • 那么,具体问题是什么?为什么要转储代码?
  • 我展示了我是如何设置摧毁敌人的。我需要的是我不知道制作它需要什么代码,所以当敌人被摧毁时,我会在那里产生一个物品
  • 您是否尝试过任何方法来实现它,或者您只是从获得的答案中复制粘贴代码?

标签: swift sprite-kit


【解决方案1】:

我会在创建时将物品附加到敌人身上,然后当它被破坏时,您可以将附加的物品移到现场,然后将敌人杀死。

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

【讨论】:

    【解决方案2】:

    您可以创建一个接收 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)
    }
    

    然后当你击中敌人时,你调用 spawn 函数传递敌人的位置。

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

    希望这能让你走上正轨。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-26
      • 1970-01-01
      • 1970-01-01
      • 2021-01-25
      • 2020-03-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多