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