[SOVED] Help with The Two Flowers

So, some of my soldiers will attack, some will just stand there, my hero just runs around collecting coins, and Hector always dies.

// If the peasant is damaged, the flowers will shrink!

function summonSoldiers() {
    if (hero.gold >= hero.costOf("soldier")) {
        hero.summon("soldier");
    }
}

// Define the function: commandSoldiers
function commandSoldiers() {
    var soldiers = hero.findByType("soldier", hero.findFriends());
    var soldierIndex =0;
    for(var i =0; i< soldiers.length; i++){
        var soldier = soldiers[soldierIndex];
        var enemy = soldier.findNearestEnemy();
        if(enemy){
       hero.command(soldier, 'attack', enemy);
        }
    }
    }

// Define the function: pickUpNearestCoin
function pickUpNearestCoin(){
    var coin = hero.findNearestItem();
    if (coin){
        hero.move(coin.pos);
    }
}
var peasant = hero.findByType("peasant")[0];

while(true) {
    summonSoldiers();
    // commandSoldiers();
    commandSoldiers();
    // pickUpNearestCoin();
    pickUpNearestCoin();
    
}

1.You do not have to define soldierIndex you have i that is the same thing.
So it should be var soldier = soldiers[i];and you should delete var soldierIndex =0; you define index inside for loop.

yay that worked! you rock <3

1 Like