Tatical Strike Can't kill 2nd Enemy


#1

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();

#2

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.


#3

How would I do it with a while loop?

// Defeat the ogres.
hero.moveUp();
hero.moveRight(3);
hero.moveDown(2);
while (true) {
var enemy = hero.findNearestEnemy();
    hero.attack(enemy);
}
hero.moveLeft();
hero.moveDown();

With this way he doesn’t break the loop after the enemies are dead. Instead I walk into the wall looking for more enemies.


#4

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.


#5

Alright well at least I solved it the other way but thank you!


#6

Here ill explain whats going on.

Ill use random names just for explaining.

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.


#7

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?


#8
var enemy = hero.findNearestEnemy(); #Enemy = Bill
var enemy1 = hero.findNearestEnemy(); #Enemy = Jill
    hero.attack(enemy); # Attack Bill
    hero.attack(enemy1); # Attack Jill

This will not work .findNearestEnemy() finds the closest enemy to you. If you dont change your position then you would be referring to the same enemy.


#9

Alright I think I am getting thanks a lot!

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?


#11

I updated my code. Sorry I made a mistake. I only updated the variables.

Ignore the (.'s) Thats only for indentation.


#12

You can initalize variables outside of a while loop. Global scope refers to anything on the outter most bracket.

To keep it simple while global can be very complex.

var Enemy = "Bill"

while (True)
{
#This while loop can use Enemy.
}

#13

Ok so if I understand correctly. All I needed to do was reset the variable not change the name of the variable?

So for example

var enemy = hero.findNearestEnemy(); #Enemy = Bill Can be created once? But I can use it over and over and it will just reset?

So then

var enemy = hero.findNearestEnemy(); #Enemy = Bill Set’s the target to Bill
hero.attack(enemy); # Attack Bill Then attacks Bill

Then I reset the variable using the already existing variable right?

var enemy = hero.findNearestEnemy(); #Enemy = Jill This time I reset the variable and it’s focusing the next closest enemy in this case Jill?

hero.attack(enemy); # Attack Jill And now since the variable is set to attack Jill because she is the closest. I now attack and kill her right?


#14

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


#15

Right as long as he is dead in this level’s case it only takes one hit but if It took 2 hits and I didn’t use

hero.attack(enemy);
hero.attack(enemy);

twice then I would just stop attacking and get killed right?


#16

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.


#17

So then this would work if I needed to attack an enemy twice to kill it, then move onto the next enemy and do the same?

var enemy = hero.findNearestEnemy(); #Enemy = Bill
hero.attack(enemy); #Attack Bill
hero.attack(enemy); #Attack Bill

enemy = hero.findNearestEnemy(): #Enemy = Jill
hero.attack(enemy); #Attack Jill
hero.attack(enemy); #Attack Jill

#18

Yes that would work.


#19

Sweet! Thanks for the help man I really appreciate it! I am on the final challenge of this area now thanks to your explanations :D!


#20

Keep up the good work. :smiley: