Cleave multiple enemies

Hi evertyone, I am trying to cleave multiple enemies at Backwoods Brawl, but keep getting TypeError: Cannot read property ‘type’ of undefined for this line:
elif (hero.distanceTo(enemies[countEnemies]) < 10):

What has gone wrong?

while True:
    countEnemies = 0
    nearEnemies = 0
    enemies = hero.findEnemies()
    while (countEnemies < enemies.length):
        countEnemies +=1
        if (enemies[countEnemies].type == "thrower"):
            while enemies[countEnemies].health > 0:
                hero.attack(enemies[countEnemies])
        elif (hero.distanceTo(enemies[countEnemies]) < 10):
            nearEnemies += 1
            hero.say("I count" + nearEnemies)
    if nearEnemies > 4 and hero.isReady("cleave"):
        hero.cleave()
    if hero.findNearest():
        hero.attack(hero.findNearestEnemy())
    else:
        hero.shield()

Why are you putting it in parethesis

1 Like

You should define what your hero is attacking in hero.findNearest(). You also put parenthesis in the places you shouldn’t have put in. I think that this is causing the error.

1 Like

Thank you! I removed the parenthesis and added Enemy to findNearest, which fixed the error, but apparently, I am writing the looping through the array in a wrong way since the character is now just standing there. Is it covered during the later levels?

What I want it to do is to attack throwers and do mass cleave when multiple enemies are around.

while True:
    countEnemies = 0
    nearEnemies = 0
    enemies = hero.findEnemies()
    while countEnemies < enemies.length:
        if enemies[countEnemies].type == "thrower":
            while enemies[countEnemies].health > 0:
                hero.attack(enemies[countEnemies])
        if hero.distanceTo(enemies[countEnemies]) < 10:
            nearEnemies += 1
            hero.say("I count" + nearEnemies)
            if nearEnemies > 4 and hero.isReady("cleave"):
                hero.cleave()
            if hero.findNearestEnemy():
                hero.attack(hero.findNearestEnemy())
        countEnemies +=1        
    else:
        hero.shield()

I suggest you get rid of hero.say because it slows you down and you can’t really do anything while speaking