Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

Convenient Enemy Problem

My character summons the troops and defeats their enemies, but my guy doesn’t attack at the last part! Can anyone help?!


for (var x = 8; x <= 72; x += 16) {
    hero.moveXY(x, 22);
    // Peasants know whom to summon.
    var peasant = hero.findNearest(hero.findFriends());
    var message = peasant.message;
     if (message) {
        // Words are seaparated by whitespaces.
        var words = message.split(" ");
        // "words" is an array of words from the "message".
        // Get the last word. It's the required type.
        var type =  words[words.length - 1];
        // Summon the required unit type.
        hero.summon(type);
        
    }
}
for (var i = 0; i < hero.built.length; i++) {
    var unit = hero.built[i];
    // Command the unit to defend the unit's position.
    hero.command(unit, "defend", unit.pos);
}

var enemy = hero.findNearestEnemy();
if (enemy){
    hero.attack(enemy);
}

3 Likes

Are you using a while-true loop for the hero’s attack method?

3 Likes

gimme a screenshot.:four_leaf_clover:

2 Likes

i tried your code but my hero just runs thru all of the Xs and spawns 2 archers on the same X

1 Like

if you say the type like this

hero.summon(type);
hero.say(type);

it works

2 Likes

so …it doesnt work

1 Like

i tryed it and it desnt work

1 Like

What happens? Does an error show up or is the code just not working right?

1 Like

its just not working

1 Like

1 Like

Lines 24-28: What is enemy? Define enemy inside the loop so that the code doesn’t just run once and stop.

2 Likes

ok ill see what i can do with that

2 Likes