# Tatical Strike Can't kill 2nd Enemy

**URL:** <https://discourse.codecombat.com/t/tatical-strike-cant-kill-2nd-enemy/10169>\
**Category:** Uncategorized\
**Created:** [January 3, 2017, 10:15pm UTC](https://discourse.codecombat.com/t/tatical-strike-cant-kill-2nd-enemy/10169 "2017-01-03T22:15:26Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cydo](https://avatars.discourse-cdn.com/v4/letter/c/f4b2a3/32.png) [@Cydo](https://discourse.codecombat.com/u/Cydo)\
**Post date:** [January 3, 2017, 10:15pm UTC](https://discourse.codecombat.com/t/tatical-strike-cant-kill-2nd-enemy/10169/1 "2017-01-03T22:15:26Z")

</div>

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?

```python
// 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?

```python
// 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();

```

---

<div class="post-metadata">

**Author:** ![Ty\_Ler](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ty_ler/32/5322_2.png) [@Ty\_Ler](https://discourse.codecombat.com/u/Ty_Ler)\
**Post date:** [January 3, 2017, 10:20pm UTC](https://discourse.codecombat.com/t/tatical-strike-cant-kill-2nd-enemy/10169/2 "2017-01-03T22:20:59Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Cydo](https://avatars.discourse-cdn.com/v4/letter/c/f4b2a3/32.png) [@Cydo](https://discourse.codecombat.com/u/Cydo)\
**Post date:** [January 3, 2017, 10:24pm UTC](https://discourse.codecombat.com/t/tatical-strike-cant-kill-2nd-enemy/10169/3 "2017-01-03T22:24:50Z")

</div>

How would I do it with a while loop?

```python
// 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.

---

<div class="post-metadata">

**Author:** ![Ty\_Ler](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ty_ler/32/5322_2.png) [@Ty\_Ler](https://discourse.codecombat.com/u/Ty_Ler)\
**Post date:** [January 3, 2017, 10:25pm UTC](https://discourse.codecombat.com/t/tatical-strike-cant-kill-2nd-enemy/10169/4 "2017-01-03T22:25:51Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Cydo](https://avatars.discourse-cdn.com/v4/letter/c/f4b2a3/32.png) [@Cydo](https://discourse.codecombat.com/u/Cydo)\
**Post date:** [January 3, 2017, 10:27pm UTC](https://discourse.codecombat.com/t/tatical-strike-cant-kill-2nd-enemy/10169/5 "2017-01-03T22:27:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Ty\_Ler](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ty_ler/32/5322_2.png) [@Ty\_Ler](https://discourse.codecombat.com/u/Ty_Ler)\
**Post date:** [January 3, 2017, 10:28pm UTC](https://discourse.codecombat.com/t/tatical-strike-cant-kill-2nd-enemy/10169/6 "2017-01-03T22:28:34Z")

</div>

Here ill explain whats going on.

Ill use random names just for explaining.

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

```

```python
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

```python
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.

---

<div class="post-metadata">

**Author:** ![Cydo](https://avatars.discourse-cdn.com/v4/letter/c/f4b2a3/32.png) [@Cydo](https://discourse.codecombat.com/u/Cydo)\
**Post date:** [January 3, 2017, 10:33pm UTC](https://discourse.codecombat.com/t/tatical-strike-cant-kill-2nd-enemy/10169/7 "2017-01-03T22:33:20Z")

</div>

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.

```python

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?

```python
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?

---

<div class="post-metadata">

**Author:** ![Ty\_Ler](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ty_ler/32/5322_2.png) [@Ty\_Ler](https://discourse.codecombat.com/u/Ty_Ler)\
**Post date:** [January 3, 2017, 10:36pm UTC](https://discourse.codecombat.com/t/tatical-strike-cant-kill-2nd-enemy/10169/8 "2017-01-03T22:36:04Z")

</div>

```python
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.

---

<div class="post-metadata">

**Author:** ![Cydo](https://avatars.discourse-cdn.com/v4/letter/c/f4b2a3/32.png) [@Cydo](https://discourse.codecombat.com/u/Cydo)\
**Post date:** [January 3, 2017, 10:37pm UTC](https://discourse.codecombat.com/t/tatical-strike-cant-kill-2nd-enemy/10169/9 "2017-01-03T22:37:10Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Ty\_Ler](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ty_ler/32/5322_2.png) [@Ty\_Ler](https://discourse.codecombat.com/u/Ty_Ler)\
**Post date:** [January 3, 2017, 10:42pm UTC](https://discourse.codecombat.com/t/tatical-strike-cant-kill-2nd-enemy/10169/11 "2017-01-03T22:42:48Z")

</div>

> [@Ty\_Ler](#):
>
> 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.

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

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

---

<div class="post-metadata">

**Author:** ![Ty\_Ler](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ty_ler/32/5322_2.png) [@Ty\_Ler](https://discourse.codecombat.com/u/Ty_Ler)\
**Post date:** [January 3, 2017, 10:49pm UTC](https://discourse.codecombat.com/t/tatical-strike-cant-kill-2nd-enemy/10169/12 "2017-01-03T22:49:50Z")

</div>

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.

```python
var Enemy = "Bill"

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

```

---

<div class="post-metadata">

**Author:** ![Cydo](https://avatars.discourse-cdn.com/v4/letter/c/f4b2a3/32.png) [@Cydo](https://discourse.codecombat.com/u/Cydo)\
**Post date:** [January 3, 2017, 10:50pm UTC](https://discourse.codecombat.com/t/tatical-strike-cant-kill-2nd-enemy/10169/13 "2017-01-03T22:50:21Z")

</div>

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?**

---

<div class="post-metadata">

**Author:** ![Ty\_Ler](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ty_ler/32/5322_2.png) [@Ty\_Ler](https://discourse.codecombat.com/u/Ty_Ler)\
**Post date:** [January 3, 2017, 10:51pm UTC](https://discourse.codecombat.com/t/tatical-strike-cant-kill-2nd-enemy/10169/14 "2017-01-03T22:51:51Z")

</div>

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`

---

<div class="post-metadata">

**Author:** ![Cydo](https://avatars.discourse-cdn.com/v4/letter/c/f4b2a3/32.png) [@Cydo](https://discourse.codecombat.com/u/Cydo)\
**Post date:** [January 3, 2017, 10:53pm UTC](https://discourse.codecombat.com/t/tatical-strike-cant-kill-2nd-enemy/10169/15 "2017-01-03T22:53:17Z")

</div>

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

```python
hero.attack(enemy);
hero.attack(enemy);

```

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

---

<div class="post-metadata">

**Author:** ![Ty\_Ler](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ty_ler/32/5322_2.png) [@Ty\_Ler](https://discourse.codecombat.com/u/Ty_Ler)\
**Post date:** [January 3, 2017, 10:56pm UTC](https://discourse.codecombat.com/t/tatical-strike-cant-kill-2nd-enemy/10169/16 "2017-01-03T22:56:57Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Cydo](https://avatars.discourse-cdn.com/v4/letter/c/f4b2a3/32.png) [@Cydo](https://discourse.codecombat.com/u/Cydo)\
**Post date:** [January 3, 2017, 10:59pm UTC](https://discourse.codecombat.com/t/tatical-strike-cant-kill-2nd-enemy/10169/17 "2017-01-03T22:59:42Z")

</div>

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?

```python
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

```

---

<div class="post-metadata">

**Author:** ![Ty\_Ler](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ty_ler/32/5322_2.png) [@Ty\_Ler](https://discourse.codecombat.com/u/Ty_Ler)\
**Post date:** [January 3, 2017, 11:00pm UTC](https://discourse.codecombat.com/t/tatical-strike-cant-kill-2nd-enemy/10169/18 "2017-01-03T23:00:19Z")

</div>

Yes that would work.

---

<div class="post-metadata">

**Author:** ![Cydo](https://avatars.discourse-cdn.com/v4/letter/c/f4b2a3/32.png) [@Cydo](https://discourse.codecombat.com/u/Cydo)\
**Post date:** [January 3, 2017, 11:01pm UTC](https://discourse.codecombat.com/t/tatical-strike-cant-kill-2nd-enemy/10169/19 "2017-01-03T23:01:08Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Ty\_Ler](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ty_ler/32/5322_2.png) [@Ty\_Ler](https://discourse.codecombat.com/u/Ty_Ler)\
**Post date:** [January 3, 2017, 11:03pm UTC](https://discourse.codecombat.com/t/tatical-strike-cant-kill-2nd-enemy/10169/20 "2017-01-03T23:03:32Z")

</div>

Keep up the good work. 😃
