// This code runs once per frame. Choose where to move to grab gold!
// First player to 150 gold wins.
// This is an example of grabbing the 0th coin from the items array.
var items = this.getItems();
var i = 0;
if (items[i]) {
if (items[i].bountyGold >= 3) {
this.move(items[i].pos);
} else {
i++;
}
} else {
i = 0;
}
// Click on a coin to see its API.
Hey there can some of you fine ladies and fellas please read my code and tell me what the dump I’m doing wrong? As you can see it runs once per frame. I’m a real n00b and I apologize for that but can someone help me out here? Thanks!
well, if the code were run from the top every frame, “i” would always be zero…
var i = 0
you need to loop over the coins and pick a different coin (closer and/or move valuable) to collect otherwise you will always run after the first coin in the array.
What happens if there are two coins with bountyGold > 3 && distance <= 40?
The moveXY command will be called multiple times. Thus you set the desired action more than once per frame. The game warns you that something unexpected may happen.
In your case that simply is that you will collect the last coin to which the above conditions apply first.
sounds like it really is running “once” per frame.
the error/warning says that you can only make one action . . . since your “action” (moveXY) is in the loop you issue several before the loop is done. pick the one you want then when the loop is done move to it
Alright so I had no success moving the “moveXY” outside of the loop. All my character did then was continuously travel off of the right side of the screen. So I tried scrapping all of that and taking an entirely different approach. Still without any luck. Why doesn’t this work. I feel like it should. The code is looped automatically and I am not sure how to circumvent that.
this.itemsDistances = function() {
for (var i = 0; i < this.getItems().length; i++) {
return this.distance(this.getItems[i]);
}
};
Array.min = function( array ){
return Math.min.apply( Math, array );
};
for (var j = 0; j < this.getItems().length; j++) {
if (Array.min(this.itemsDistances) == this.getItems[j]) {
this.move(this.getItems[j].pos);
}
}
Seriously, indentations please! Use them right, then they are your friends, otherwise your worst foes.
Okay, maybe not in this case, but still!
I’m not entirely sure what happens in your code. But from what I can see, this.itemDistances() will always return the distance to the first item (at the time where the return statement is executed i == 0).
You then (over-?)write a function to Array.min. I’m not sure, what Math.min.apply does, but I’m pretty sure it can’t apply the whole Math-library to the array (or vice versa).
Given that your intention with Array.min is to get the element with the smallest distance, then and only then would your last for-loop work.
I call it very unlikely that your last for-loops works. Do you get any error? Red circles below your characters feet or something?
#written in pseudo-python (indent is like javascript: { code; }; )
index = 0
bestDistance = 9999
closestItem = -1
while index < length of itemArray
currentItem = itemArray[index]
if currentItem distance < bestDistance:
closestItem = index
bestDistance = currentItem distance
index += 1
# closestItem is either -1 or it was set to the index of the closest item
if closestIem >= 0
moveXY(itemArray[closestItem].pos)
else
say "Ack Phbt, I can't see any items"