# This.distanceTo(anything) glitch

**URL:** <https://discourse.codecombat.com/t/this-distanceto-anything-glitch/2499>\
**Category:** Uncategorized\
**Created:** [January 29, 2015, 8:44pm UTC](https://discourse.codecombat.com/t/this-distanceto-anything-glitch/2499 "2015-01-29T20:44:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dominion](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/dominion/32/1571_2.png) [@Dominion](https://discourse.codecombat.com/u/Dominion)\
**Post date:** [January 29, 2015, 8:44pm UTC](https://discourse.codecombat.com/t/this-distanceto-anything-glitch/2499/1 "2015-01-29T20:44:09Z")

</div>

Hey i’m back. I was having a lot of trouble on the levels so i went back to the basics to get better. Any way i have been playing the new campaign(btw finally) and i noticed in one level the code:

```python
loop {
    var enemy = this.findNearestEnemy();
    var distance = this.distanceTo(enemy);
    if (distance < 10) {
        this.attack(enemy);
        
    }
else {
    this.moveXY(40, 37);

```

but now in later levels it doesn’t work it says target is not specified, null or something along those lines and i dont know why. in the codex or whatever that book is called it has this code

```python
else if (this.isReady("cleave") && this.distanceTo(enemy) < 10) {
    this.cleave(enemy);
}

```

and i doesn’t work no matter what i do like make a distance variable. i dont understand also recently the `var item = this.findNearestItem();` doesn’t work anymore but it was working fine a day ago either im doing something wrong or the code in the campaign is messed up

---

<div class="post-metadata">

**Author:** ![sotonin](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/sotonin/32/24089_2.png) [@sotonin](https://discourse.codecombat.com/u/sotonin)\
**Post date:** [January 29, 2015, 8:50pm UTC](https://discourse.codecombat.com/t/this-distanceto-anything-glitch/2499/2 "2015-01-29T20:50:16Z")

</div>

You probably got upgraded glasses and no longer have access to the findNearestEnemy function.

You’d need to convert that to

```python
this.findNearest(this.findEnemies());

```

---

<div class="post-metadata">

**Author:** ![Vettax](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/vettax/32/1514_2.png) [@Vettax](https://discourse.codecombat.com/u/Vettax)\
**Post date:** [January 29, 2015, 10:29pm UTC](https://discourse.codecombat.com/t/this-distanceto-anything-glitch/2499/3 "2015-01-29T22:29:04Z")

</div>

with:

```python
var enemy = this.findNearestEnemy();

```

enemy exist only if there is an enemy in view range.  
If there is not, you can’t get the distance to it.  
You should use something like

```python
var enemy = this.findNearestEnemy();
if (enemy)
{
    var distance = this.distanceTo(enemy);
    //do something
}

```

To complete Sotonin answer, you can use also this.findItems()
