The dunes help with code


#1

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

// 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!?)


#2

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


#3

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


#4

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})


#5

Oh ok thank you Glacian


#6

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

// 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});
        
    }
}

#7

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


#8

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

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

#9

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

this.move(enemy.pos);

#10

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


#11

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?


#12

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.


#13

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

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.


#14

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