# Recognize and attack enemies when new enemies spawn

**URL:** <https://discourse.codecombat.com/t/recognize-and-attack-enemies-when-new-enemies-spawn/12059>\
**Category:** Uncategorized\
**Created:** [July 29, 2017, 10:17pm UTC](https://discourse.codecombat.com/t/recognize-and-attack-enemies-when-new-enemies-spawn/12059 "2017-07-29T22:17:26Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![HeadMan](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/headman/32/6637_2.png) [@HeadMan](https://discourse.codecombat.com/u/HeadMan)\
**Post date:** [July 29, 2017, 10:17pm UTC](https://discourse.codecombat.com/t/recognize-and-attack-enemies-when-new-enemies-spawn/12059/1 "2017-07-29T22:17:26Z")

</div>

I know I was told this before but now I don’t know how to fix my code so that my hero will recognize when new enemies spawn and stop proceeding to XY and instead resume the attack loop now that the condition has changed back to true. I found that this fixes it, hero.move({‘x’:151 , ‘y’: 118}), but is that the only way (What if I didn’t have boots that allow for that type of movement) or is there something else that I could have done???

code

```python
while True:
        enemy = hero.findNearestEnemy()
        if enemy:
            while enemy.health > 0:
                hero.attack(enemy)
        else:
            hero.moveXY(151, 118)

```

---

<div class="post-metadata">

**Author:** ![Ironhead](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ironhead/32/7384_2.png) [@Ironhead](https://discourse.codecombat.com/u/Ironhead)\
**Post date:** [July 30, 2017, 10:53pm UTC](https://discourse.codecombat.com/t/recognize-and-attack-enemies-when-new-enemies-spawn/12059/2 "2017-07-30T22:53:56Z")

</div>

what is your code? I cant see it because it is blurred out. I might be able to help if you show the code, and by the way, dont forget to do @Ironhead because I might not be available, so just remind me. What level are you on too? Reply to these questions first.

---

<div class="post-metadata">

**Author:** ![Hellenar](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/hellenar/32/9100_2.png) [@Hellenar](https://discourse.codecombat.com/u/Hellenar)\
**Post date:** [July 31, 2017, 1:01am UTC](https://discourse.codecombat.com/t/recognize-and-attack-enemies-when-new-enemies-spawn/12059/3 "2017-07-31T01:01:47Z")

</div>

Click the code to see it.

---

<div class="post-metadata">

**Author:** ![HeadMan](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/headman/32/6637_2.png) [@HeadMan](https://discourse.codecombat.com/u/HeadMan)\
**Post date:** [July 31, 2017, 8:25am UTC](https://discourse.codecombat.com/t/recognize-and-attack-enemies-when-new-enemies-spawn/12059/4 "2017-07-31T08:25:53Z")

</div>

If I remember correctly i have to have the hero move a bit then use an if enemy fight function then move a bit and fight again. I think that’s the only way that moveXY will work.

---

<div class="post-metadata">

**Author:** ![Ironhead](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ironhead/32/7384_2.png) [@Ironhead](https://discourse.codecombat.com/u/Ironhead)\
**Post date:** [July 31, 2017, 2:23pm UTC](https://discourse.codecombat.com/t/recognize-and-attack-enemies-when-new-enemies-spawn/12059/5 "2017-07-31T14:23:46Z")

</div>

code is too blurry for me to see

---

<div class="post-metadata">

**Author:** ![Hellenar](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/hellenar/32/9100_2.png) [@Hellenar](https://discourse.codecombat.com/u/Hellenar)\
**Post date:** [July 31, 2017, 4:49pm UTC](https://discourse.codecombat.com/t/recognize-and-attack-enemies-when-new-enemies-spawn/12059/6 "2017-07-31T16:49:06Z")

</div>

Click on the code and it will show up more clearly.

---

<div class="post-metadata">

**Author:** ![Ironhead](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ironhead/32/7384_2.png) [@Ironhead](https://discourse.codecombat.com/u/Ironhead)\
**Post date:** [July 31, 2017, 8:58pm UTC](https://discourse.codecombat.com/t/recognize-and-attack-enemies-when-new-enemies-spawn/12059/7 "2017-07-31T20:58:31Z")

</div>

i did still blurry (20 characters)

---

<div class="post-metadata">

**Author:** ![Ironhead](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ironhead/32/7384_2.png) [@Ironhead](https://discourse.codecombat.com/u/Ironhead)\
**Post date:** [July 31, 2017, 8:59pm UTC](https://discourse.codecombat.com/t/recognize-and-attack-enemies-when-new-enemies-spawn/12059/8 "2017-07-31T20:59:51Z")

</div>

nevermind, @HeadMan, there aren’t many ways, but yours works very well. You could also use a flag and if enemy just click you hero’s location so he can start fighting.

---

<div class="post-metadata">

**Author:** ![Ironhead](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ironhead/32/7384_2.png) [@Ironhead](https://discourse.codecombat.com/u/Ironhead)\
**Post date:** [July 31, 2017, 9:00pm UTC](https://discourse.codecombat.com/t/recognize-and-attack-enemies-when-new-enemies-spawn/12059/9 "2017-07-31T21:00:07Z")

</div>

I use this strategy a lot and it helps
