while(true) {
var enemy = hero.findNearestEnemy();
if (hero.isReady("cleave")) {
hero.cleave(enemy);
}
else {
var enemy=hero.findNearestEnemy();
if (enemy) {
hero.attack(enemy);
}
}
}
With that code hero live a little bit longer but munchkin killing him anyway.
Any idea what I’m doing wrong? Armor i think is ok long sword with the cleave function. I don’t see any solution in FAQ
First check if an enemy exists.
If an enemy exists:
then check if your cleave ability is ready.
If it is ready:
cleave the enemy.
Else:
just attack.
The FAQ does not provide solutions to levels. The purpose of CodeCombat is to help people learn and not just copy off of others. It does, however, provide helpful advice and tips for those who require general assistance.
I don’t want copy anything Just want to understand
becouse I think my code should run correctly.
I check if an enemy exist
then check cleave ability
if it’s redy hero cleave
else
hero attack.
But as allways they are killing him and I don’t know why
while (true) {
if (enemy) {
var enemy = hero.findNearestEnemy();
if (hero.isReady("clave"))
}
else {
if (enemy) {
hero.attack(enemy);
}
}
if I’m writing if enemy just in" if" section , red warning sign shown off. I added another one if enemy on "else "section as whose recommended but still nothing changed. hero is dead . It’s just a simple loop what is going on
In the if (enemy) statement you forgot a starting bracket: {.
It should look like if (enemy){
Make sure you also add an end bracket to close the if statement: }
while (true) {
if (enemy) {
if (hero.isReady("cleave")) {
var enemy = hero.findNearestEnemy();
hero.cleave(enemy);
}
} else {
if (enemy) {
hero.attack(enemy);
}
}
}
You don’t need that second if (enemy) {. The else-statement matches up with the if (hero.isReady("cleave") {, not the if (enemy) {.
Also the var enemy… is the first statement in the loop, and THEN the rest follows.
while (true) {
var enemy = hero.findNearestEnemy();
if (enemy){
if (hero.isReady("cleave")) {
hero.cleave(enemy);
}
} else {
hero.attack(enemy);
}
}
I write something like this .
var statement is the first in the loop I deleted second if(enemy).
I suppose the problem is with the “{ }” something is wrong becouse hero cleave just one and doesn’t attack …
You need to wrap the entire thing in the if (enemy) statement:
[while loop]
[define variable enemy]
[if enemy]
[if cleave is ready]
then cleave
}else{
then attack
[close bracket for if enemy]
[close bracket for while]
The logic behind this is that this will infinite loop because true is always true. Then you define enemy, and check if there is one. If there is no enemy, your hero will continue to loop and do nothing until one appears. Once an enemy spawns, you check if your cleave has recharged again. If so, cleave, otherwise, just regularly attack (which has no cooldown)
First: hero.isReady needs a starting bracket if(hero.isReady("cleave")){
Then you need to add another end bracket to close the cleave. Then, indent the else statement and everything inside it.
Ok, now I understand.
So the problem should be with “{}” in my code.
Now the code should run properly. Hero cleave and attack more then one time, live longer but not long enought to win the fight
How much health do you have. That may be the problem. If that is the problem you should buy better armor with your gems. Also you may need to line up that else statement with the if statement.