I haven’t done this yet being that I am not that far yet since I’m stuck somewhere else but I formatted your code for you and I’ll give you my solution to the level. I think you have your code written with too many if’s and else’s causing an error because it’s looking for and else if statement but I am unsure but try this.
while(true) {
var currentHealth = hero.health;
var healingThreshold = hero.maxHealth / 2;
var enemy = hero.findNearestEnemy()
if (hero.health < hero.maxHealth)
hero.moveXY(65, 46);
hero.say("heal me");
else {
if (enemy) {
if hero.isReady("cleave");
hero.cleave(enemy);
}
else {
hero.attack(enemy);
else {
hero.attack(enemy);
}
In your code I would of written in this way:
while(true) {
var currentHealth = hero.health;
var healingThreshold = hero.maxHealth / 2;
var enemy = hero.findNearestEnemy()
if (hero.health < hero.maxHealth) {
hero.moveXY(65, 46);
hero.say("heal me");
} else if (enemy && hero.isReady("cleave")) {
hero.cleave(enemy);
} else {
hero.attack(enemy);
}
}
I will break it down for you on why I wrote it the way I did and you can let me know if it works for you.
first you assigned the variables like so
var currentHealth = hero.health;
var healingThreshold = hero.maxHealth / 2;
var enemy = hero.findNearestEnemy()
then you have to make an if statement that would move your character to a certain coordinate and ask to be healed when your health is at a certain number that is less then your max health.
if (hero.health < hero.maxHealth) {
hero.moveXY(65, 46);
hero.say("heal me");
}
then you had to write and else if statement that would cleave any enemies as long as cleave was ready to be used like so
} else if (enemy && hero.isReady("cleave")) {
hero.cleave(enemy);
The reason I wrote it this way is because from what I have learned you write if statements like this: you start with a plain if statement followed by else if then by else. But you can have as many else if statements between the starting if statement and the ending else statement as you want for example:
if (hero.health < hero.maxHealth) {
hero.moveXY(65, 46);
hero.say("heal me");
} else if (enemy && hero.isReady("cleave")) {
hero.cleave(enemy);
} else if (enemyDistance > 10 && hero.health < hero.maxHealth) {
hero.shield();
} else {
hero.attack(enemy);
}
So pretty you start and if statement like so.
if (hero.health < hero.maxHealth) {
hero.moveXY(65, 46);
hero.say("heal me");
Than anything in here can be else if statements of w.e you like:
else if statements
else if statements
else if statements
else if statements
else if statements
finally ending with just a plain old else statement
else {
hero.attack(enemy);
}
Well let me know if my explanation helped you. I’m still learning as well but this is how I would of done your challenge and the reasoning behind it. Also for future reference you highlight your code and click the </> to put it in a code block so it is easily readable or you can type ``` before and after your code.