while (true) {
var friends = hero.findFriends();
// Use for-loop and for each friend:
for (var i = 0; i < friends.length; i ++) {
var enemy = hero.findNearestEnemy();
var friend = friends[i];
if (enemy) {
hero.command(friend, "attack", enemy);
}
else {
hero.command(friend, "move", {'x': friend.pos.x + 25, friend.pos.y});
}
}
// If they see an enemy then command to attack.
// Command to move east by small steps.
}
i don’t understand the error it say “there a problem in your code”
// You can use findNearestEnemy() on your soldiers to get their nearest enemy instead of yours.
// move forward
var pos = [];
pos[0] = {'x': 49, 'y': 66};
pos[1] = {'x': 61, 'y': 47};
pos[3] = {'x': 51, 'y': 29};
while (true) {
var friends = hero.findFriends();
// Use for-loop and for each friend:
// attack
var enemy = hero.findNearestEnemy();
if (enemy) {
if (hero.isReady("bash")) {
hero.bash(enemy);
}
if (hero.canCast("chain-lightning", enemy)) {
hero.cast("chain-lightning", enemy);
}
else {
hero.attack(enemy);
}
}
// command friends
for (var i = 0; i < friends.length; i ++) {
var friend = friends[i];
var enemy = friend.findNearestEnemy();
if (enemy && friend.type == "soldier") {
hero.command(friend, "attack", enemy);
}
if (enemy && friend.type == "archer") {
if (hero.distanceTo(enemy) < 7) {
hero.command(friend, "attack", enemy);
}
}
else {
hero.command(friend, "move", {'x': friend.pos.x + parseFloat(0.27), 'y': friend.pos.y});
}
}
// If they see an enemy then command to attack.
// Command to move east by small steps.
}
Look what, it doesn’t really matter the type of the friend, so just make sure the friend exist like you did with enemy, and if you want your hero to attack, let the hero move with the troops in the middle.
and this can simply be 1, no need to complicate stuff
and here make an else for your hero to move with the troops
// You can use findNearestEnemy() on your soldiers to get their nearest enemy instead of yours.
// move forward
var pos = [];
pos[0] = {'x': 49, 'y': 66};
pos[1] = {'x': 61, 'y': 47};
pos[3] = {'x': 51, 'y': 29};
while (true) {
var friends = hero.findFriends();
// Use for-loop and for each friend:
// attack
var enemy = hero.findNearestEnemy();
if (enemy) {
if (hero.isReady("bash")) {
hero.bash(enemy);
}
if (hero.canCast("chain-lightning", enemy)) {
hero.cast("chain-lightning", enemy);
}
else {
hero.attack(enemy);
hero.move(friend.pos);
}
}
// command friends
for (var i = 0; i < friends.length; i ++) {
var friend = friends[i];
var enemy = friend.findNearestEnemy();
if (enemy && friend.type == "soldier") {
hero.command(friend, "attack", enemy);
}
if (enemy && friend.type == "archer") {
if (hero.distanceTo(enemy) < 7) {
hero.command(friend, "attack", enemy);
}
}
else {
hero.command(friend, "move", {'x': friend.pos.x + 1, 'y': friend.pos.y});
}
}
// If they see an enemy then command to attack.
// Command to move east by small steps.
}