I have tried this multiple ways and no matter what I complete the level everything but fail to kill the second enemy. Can someone explain to me what I am doing wrong?
// Defeat the ogres.
hero.moveUp();
hero.moveRight(3);
hero.moveDown(2);
var enemy = hero.findNearestEnemy();
hero.attack(enemy);
hero.attack(enemy);
hero.moveLeft();
hero.moveDown();
//EDIT: I solved it but I am unsure as to why this worked opposed to the could I previously stated. Does the variable I made only work for the one instance of attack? Then I have to reinitialize with a new variable to attack the same enemy? But if I had written the code myself and given the enemies the same name then the original code would of worked?
// Defeat the ogres.
hero.moveUp();
hero.moveRight(3);
hero.moveDown(2);
var enemy = hero.findNearestEnemy();
hero.attack(enemy);
var enemy1 = hero.findNearestEnemy();
hero.attack(enemy1);
hero.moveLeft();
hero.moveDown();
When you use the attack method twice you are refering to the same Enemy. You need to re update the value of enemy. Use a loop or call the assignment again to give enemy a new value.
You dont need to make enemy and enemy1. You can re use the variable enemy.
I take that back dont use a while loop on this map. That loop will only work if you manage to kill the first enemy before the next iteration of the loop.
var enemy = hero.findNearestEnemy(); #Enemy = Bill
hero.attack(enemy); # Attack Bill
var enemy = hero.findNearestEnemy(); #Enemy = Jill
hero.attack(enemy); # Attack Jill
var enemy = hero.findNearestEnemy(); #Enemy = Bill
hero.attack(enemy); #Attack Bill
hero.attack(enemy); #Attack Bill
Once you have finished Bill you needed to re update the variable Enemy in order to get the next one. That is why your updated code works. Changing the variable name didnt solve it. It was because you up dated the name inside Enemy.
.
.
.
Just keep in mind
var enemy = hero.findNearestEnemy(); #Enemy = Bill
hero.attack(enemy); # Attack Bill
var enemy = hero.findNearestEnemy(); #Enemy = Jill
hero.attack(enemy); # Attack Jill
This only works because Bill was dead. If he was still alive this would not work.
I get it! Thanks that makes a lot more sense. I thought the hero.findNearestEnemy(); would just find the next nearest enemy with out having to make a new variable. Also I have to have it written the right way or it wont work right?
For Example: This will work like you stated.
var enemy = hero.findNearestEnemy(); #Enemy = Bill
hero.attack(enemy); # Attack Bill
var enemy1 = hero.findNearestEnemy(); #Enemy = Jill
hero.attack(enemy1); # Attack Jill
But this will not?
var enemy = hero.findNearestEnemy(); #Enemy = Bill
var enemy1 = hero.findNearestEnemy(); #Enemy = Jill
hero.attack(enemy); # Attack Bill
hero.attack(enemy1); # Attack Jill
or will that work and it doesnt matter where I make the variables just as long as the parameter(I think thats what “enemy” is called) is corresponding correctly with the new hero.attack(); function?
EDIT: Actually one last question why can’/t I initialize variables outside of the while loop? Aren’t any variables initialized outside of something in the global scope and thus are allowed to be accessed in anything? Or is the way the game is set up preventing the use of global scope? Only letting you use local scope?
Yes this only works if you managed to kill Bill. If Bill is still alive this wont work only because Bill would still be considered the closest enemy to you.
Note you dont need to use Var again since Enemy was an already created variable. In this case you can just say enemy = hero.findNearestEnemy(); #Enemy = Jill
Yes. So if it took 2 hits to kill an enemy you would need to find a way to call the attack method 4 times. If you only called the attack method twice then they would kill you.