# The dunes help with code

**URL:** <https://discourse.codecombat.com/t/the-dunes-help-with-code/2819>\
**Category:** Uncategorized\
**Created:** [February 23, 2015, 1:56am UTC](https://discourse.codecombat.com/t/the-dunes-help-with-code/2819 "2015-02-23T01:56:45Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Feinty](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/feinty/32/1869_2.png) [@Feinty](https://discourse.codecombat.com/u/Feinty)\
**Post date:** [February 23, 2015, 1:56am UTC](https://discourse.codecombat.com/t/the-dunes-help-with-code/2819/1 "2015-02-23T01:56:45Z")

</div>

Heylo I wanted to do the dunes since I skipped it over went I edited the code a bit but now my hero just stands their with a red x under him here is my code

```python
// Collect coins. Ignore sand yaks and burls. Fight throwers and ogres.
loop {
    var enemy = this.findNearestEnemy();
    var item = this.findNearestItem();
    if (enemy) {
        if (enemy.type == "sand-yak" || enemy.type == "burl") {
            var position = this.findNearestItem();
            var x = position.x;
            var y = position.y;
            this.moveXY(x, y);
            
        }
        // But if the enemy is type "thrower" or "ogre", attack them.
        
    } else if (item) {
        position = item.pos;
        var x = position.x;
        var y = position.y;
        this.move(x,y);
        
    }
}

```

(JFBM I used backticks aren’t you pround of me!?)

---

<div class="post-metadata">

**Author:** ![Glacian](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/glacian/32/2112_2.png) [@Glacian](https://discourse.codecombat.com/u/Glacian)\
**Post date:** [February 23, 2015, 2:10am UTC](https://discourse.codecombat.com/t/the-dunes-help-with-code/2819/2 "2015-02-23T02:10:23Z")

</div>

var position = this.findNearestItem(), should be var position = item.pos;  
and at the bottom, this.move(x,y) should be this.moveXY(x,y)

---

<div class="post-metadata">

**Author:** ![Feinty](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/feinty/32/1869_2.png) [@Feinty](https://discourse.codecombat.com/u/Feinty)\
**Post date:** [February 23, 2015, 2:11am UTC](https://discourse.codecombat.com/t/the-dunes-help-with-code/2819/3 "2015-02-23T02:11:49Z")

</div>

no I have the `this.move(x,y);` boots

---

<div class="post-metadata">

**Author:** ![Glacian](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/glacian/32/2112_2.png) [@Glacian](https://discourse.codecombat.com/u/Glacian)\
**Post date:** [February 23, 2015, 2:12am UTC](https://discourse.codecombat.com/t/the-dunes-help-with-code/2819/4 "2015-02-23T02:12:22Z")

</div>

then the first one is wrong, you can’t use both!  
also the move(x, y) boots are used as follows: move({“x”: x, “y”: y})

---

<div class="post-metadata">

**Author:** ![Feinty](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/feinty/32/1869_2.png) [@Feinty](https://discourse.codecombat.com/u/Feinty)\
**Post date:** [February 23, 2015, 2:13am UTC](https://discourse.codecombat.com/t/the-dunes-help-with-code/2819/5 "2015-02-23T02:13:56Z")

</div>

Oh ok thank you Glacian

---

<div class="post-metadata">

**Author:** ![Feinty](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/feinty/32/1869_2.png) [@Feinty](https://discourse.codecombat.com/u/Feinty)\
**Post date:** [February 23, 2015, 2:17am UTC](https://discourse.codecombat.com/t/the-dunes-help-with-code/2819/6 "2015-02-23T02:17:18Z")

</div>

I fixed that but my code is not working and it still has the same error here is my code

```python
// Collect coins. Ignore sand yaks and burls. Fight throwers and ogres.
loop {
    var enemy = this.findNearestEnemy();
    var item = this.findNearestItem();
    if (enemy) {
        if (enemy.type == "sand-yak" || enemy.type == "burl") {
            var position = this.findNearestItem();
            var x = position.x;
            var y = position.y;
            this.move({"x":x, "y":y});
            
        }
        // But if the enemy is type "thrower" or "ogre", attack them.
        
    } else if (item) {
        position = item.pos;
        var x = position.x;
        var y = position.y;
        this.move({"x":x, "y":y});
        
    }
}

```

---

<div class="post-metadata">

**Author:** ![Glacian](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/glacian/32/2112_2.png) [@Glacian](https://discourse.codecombat.com/u/Glacian)\
**Post date:** [February 23, 2015, 2:20am UTC](https://discourse.codecombat.com/t/the-dunes-help-with-code/2819/7 "2015-02-23T02:20:23Z")

</div>

In your first part, you still have var position = this.findNearestItem(); and then you try to do var x = position.x; but x is not a member of an item object.  
It needs to be var position = item.pos; OR var x = position.pos.x; one or the other.

I would have just written it as this.move(item.pos);

---

<div class="post-metadata">

**Author:** ![Feinty](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/feinty/32/1869_2.png) [@Feinty](https://discourse.codecombat.com/u/Feinty)\
**Post date:** [February 23, 2015, 2:25am UTC](https://discourse.codecombat.com/t/the-dunes-help-with-code/2819/8 "2015-02-23T02:25:28Z")

</div>

Okay I used the first method and I am still getting the error here is my code now

```python
loop {
    var enemy = this.findNearestEnemy();
    var item = this.findNearestItem();
    var position = item.pos;
    if (enemy) {
        if (enemy.type == "sand-yak" || enemy.type == "burl") {
            
            var x = position.x;
            var y = position.y;
            this.move({"x":x, "y":y});
            
        }
        // But if the enemy is type "thrower" or "ogre", attack them.
        
    } else if (item) {
        position = item.pos;
        x = position.x;
        y = position.y;
        this.move({"x":x, "y":y});
        
    }
}

```

---

<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:** [February 23, 2015, 2:32am UTC](https://discourse.codecombat.com/t/the-dunes-help-with-code/2819/9 "2015-02-23T02:32:38Z")

</div>

You are trying to move to an items position in side the enemy if… and item doesnt necessarily exist. what you need is

```python
this.move(enemy.pos);

```

---

<div class="post-metadata">

**Author:** ![Feinty](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/feinty/32/1869_2.png) [@Feinty](https://discourse.codecombat.com/u/Feinty)\
**Post date:** [February 23, 2015, 2:33am UTC](https://discourse.codecombat.com/t/the-dunes-help-with-code/2819/10 "2015-02-23T02:33:39Z")

</div>

Where in my code would I put that?My enemy check?

---

<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:** [February 23, 2015, 2:35am UTC](https://discourse.codecombat.com/t/the-dunes-help-with-code/2819/11 "2015-02-23T02:35:04Z")

</div>

> [@Feinty](#):
>
> var position = item.pos;  
> if (enemy) {  
> if (enemy.type == “sand-yak” || enemy.type == “burl”) {
> 
> ```
> var x = position.x;
> var y = position.y;
> this.move({"x":x, "y":y});
>         
> }
> // But if the enemy is type "thrower" or "ogre", attack them.
>     
> }
> 
> ```

just look over your code and go over it slowly. you’ll clearly see position = item.pos

then you use it inside the if (enemy) block. why are you using an item position inside enemy block?

---

<div class="post-metadata">

**Author:** ![Glacian](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/glacian/32/2112_2.png) [@Glacian](https://discourse.codecombat.com/u/Glacian)\
**Post date:** [February 23, 2015, 2:36am UTC](https://discourse.codecombat.com/t/the-dunes-help-with-code/2819/12 "2015-02-23T02:36:23Z")

</div>

I think he wants to move to an item if the closest enemy is a yak or burl, at least thats what it looks like to me. But your error is now being caused by setting var position = item.pos without checking to see if you have an item first. Remember, item could be null, and you can’t access members of an object that doesn’t exist.

---

<div class="post-metadata">

**Author:** ![Glacian](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/glacian/32/2112_2.png) [@Glacian](https://discourse.codecombat.com/u/Glacian)\
**Post date:** [February 23, 2015, 2:38am UTC](https://discourse.codecombat.com/t/the-dunes-help-with-code/2819/13 "2015-02-23T02:38:15Z")

</div>

This should work. I don’t know if it works logically, but it should at least run without error.

```java
loop {
    var enemy = this.findNearestEnemy();
    var item = this.findNearestItem();
    if (enemy) {
        if (enemy.type == "sand-yak" || enemy.type == "burl") {
            if (item) {
                this.move(item.pos);
            }
        }
        // But if the enemy is type "thrower" or "ogre", attack them.
    } else if (item) {
        this.move(item.pos);
    }
}

```

Also, code combat should be telling you what the error is, you should have more than just a red circle to go on.

---

<div class="post-metadata">

**Author:** ![Feinty](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/feinty/32/1869_2.png) [@Feinty](https://discourse.codecombat.com/u/Feinty)\
**Post date:** [February 24, 2015, 3:29am UTC](https://discourse.codecombat.com/t/the-dunes-help-with-code/2819/14 "2015-02-24T03:29:06Z")

</div>

Thanks Glacian the code runs without an error I still have to do some work on the code but the code works!
