The thing is, I have no idea what code to type in the parentheses of if()
I already put the hero.findNearestEnemy; but I didnāt include it in the code above by accident.
Okā¦a function is meant to be a reusable block of code. Sometimes, you can pass a variable to/from the function, as needed. In this case, a variable is not being used, so is not required.
You call the function simply by naming it. In this lesson, the code would start with (look like):
while (true) {
if (shouldRun()) { //which tests if it is True
moveXY...
}
else {
kill it while you can
}
}
I tried doing that but it still does the same thing.
Iāll send my code againā¦
Re-post your code (all of it this time )ā¦Iāll try running it and see what happens.
function shouldRun() {
if (hero.health < hero.maxHealth / 2) {
return true;
} else {
return false;
}
}
while (true) {
if (shouldRun()) {
hero.moveXY(76, 37);
}
else {
hero.attack(enemy);
}
}
ayyyeā¦ it still doesnāt post the hero.findNearestEnemyā¦
Anyway, I put the hero.findNearestEnemy above all the code I just posted. Please add that when you run my code Ahh, now I know how to put the hero.findNearestEnemy. Hereās the code
var enemy = hero.findNearestEnemy();
// This function checks the hero's health
// and returns a Boolean value.
function shouldRun() {
if (hero.health < hero.maxHealth / 2) {
return true;
} else {
return false;
}
}
while (true) {
// Move to the X only if shouldRun() returns true
if (shouldRun()) {
hero.moveXY(76, 37);
} // Else, attack!
else {
hero.attack(enemy);
}
}
okā¦I put mine in the else clause (originally done in Python), so letās see what happens with JS.
All right
YESSS it worked. I couldnāt put this as a new post because I canāt put more than ā¦(I forgot)ā¦posts in a day (Iām a new user). Thank you so much for your help!!
LOL I did ā¦
Heh, you snuck in your replyā¦Iām deleting the code I just posted, so it wonāt be available to use as a simple copy and paste solution for the next person who gets stuck.