I am strggling to do this level, Could anyone help asap?
Here’s the level:
https://codecombat.com/play/level/mind-the-trap?
Here’s my code
// If you try to attack a distant enemy, your hero will charge toward it, ignoring all flags.
// You'll need to make sure you only attack enemies who are close to you!
while(true) {
var flag = hero.findFlag();
var enemy = hero.findNearestEnemy();
if(flag) {
// Pick up the flag.
hero.moveXY(flag.pos.x, flag.pos.y);
hero.say("I should pick up the flag.");
} else if(enemy) {
// Only attack if the enemy distance is < 10 meters
var distance = hero.distanceTo(enemy);
if (distance < 50) {
hero.attack(enemy);
}
}
}
Here’s what’s it doing: