一次碰撞,敌人的健康栏就会全部减少

时间:2019-02-13 05:52:32

标签: ios swift4

问题是,当玩家与敌人碰撞时,healthBar中敌人的健康不会降低指示的数量。它只是整体减少 我无法在func更新中找到更新敌人的Healthbar并尝试使用fund didBegin(_联系人:SKPhysicsContact)

的方法。
enter code here

func initZombie(){

      let enemy = Enemigo(imageNamed: "zombie1")

        var textures:[SKTexture] = []
        for i in 1...2 {
            textures.append(SKTexture(imageNamed: "zombie\(i)"))

        }
        let anima:SKAction = SKAction.animate(with: textures, timePerFrame: 15.0)
        enemy.run(SKAction.group([
            SKAction.repeatForever(anima),
            SKAction.speed(to: 60.0, duration: 0)


            ]),withKey: "animation")





    enemy.healt = 300
    enemy.exp = 100


    enemy.barraEnemy = SKSpriteNode(color: SKColor.green, size: enemy.barravidaSizeE)
    enemy.barraEnemyBack = SKSpriteNode(color: SKColor.red, size: CGSize(width: enemy.barravidaSizeE.width + 300, height:  enemy.barravidaSizeE.height + 30))



   enemy.barraEnemy.position = CGPoint(x: enemy.position.x , y: enemy.position.y - 150)
    enemy.barraEnemy.zPosition = 101
   enemy.barraEnemy.anchorPoint = CGPoint(x: 0.0 , y: 0.5)
    enemy.addChild(enemy.barraEnemy)

    enemy.barraEnemyBack.position = CGPoint(x: enemy.position.x , y: enemy.position.y - 150)
    enemy.barraEnemyBack.zPosition = 100
    enemy.barraEnemyBack.anchorPoint = CGPoint(x: 0.0, y: 0.5)
    enemy.addChild(enemy.barraEnemyBack)

    enemy.barraEnemy.size = CGSize(width: enemy.barravidaSizeE.width + CGFloat(enemy.healt) , height: enemy.barravidaSizeE.height + 30)

    enemy.zPosition = 100
        //enemy.position = CGPoint(x: size.width/2, y: size.height/2)
    enemy.position = CGPoint(x:random(min: -1100 , max: 1100), y: random(min: -400 , max: 400))
    enemy.name = "zombie"
    enemy.setScale(0.4)




        addChild(enemy)



        enemy.physicsBody = SKPhysicsBody.init(rectangleOf: enemy.size)
        enemy.physicsBody?.allowsRotation = true
        enemy.physicsBody?.affectedByGravity = false
        enemy.physicsBody?.categoryBitMask = 3
        enemy.physicsBody?.collisionBitMask = 1
        enemy.physicsBody?.contactTestBitMask = 1
        enemy.physicsBody?.isDynamic = true




}






    enter 
   code 
   here

func didBegin(_联系人:SKPhysicsContact){

var firstBody:SKPhysicsBody         var secondBody:SKPhysicsBody

    if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
        firstBody = contact.bodyA
        secondBody = contact.bodyB
    }else{
        firstBody = contact.bodyB
        secondBody = contact.bodyA

    }
    if firstBody.categoryBitMask == 1 && secondBody.categoryBitMask == 2 {
        life += 100

        secondBody.node?.removeFromParent()

    }
        if firstBody.categoryBitMask == 1 && secondBody.categoryBitMask == 3  {
            score += 1

            if let myEnemy = secondBody.node as? Enemigo {

                myEnemy.healt -= playerAtack

                life -= zombieAtack

                myEnemy.barraEnemy.size = CGSize(width: myEnemy.barravidaSizeE.width + CGFloat(myEnemy.healt), height: myEnemy.barravidaSizeE.height)



                print(myEnemy.healt)
                if myEnemy.healt <= 0 {
                    playerAtack += 10
                    life += 100
                    spawnItem(point: secondBody.node!.position)
                    secondBody.node?.removeFromParent()



                }
            }
        }
    }

0 个答案:

没有答案