…Okay, so there’s a piece of code. It’s not particularly useful, as it only provides information for flags. And it’s not even formatted properly! Why have you posted this code?
My hero walks to the nearest coin but everything just explodes…
code:
// This field is covered in firetraps. Thankfully we've sent a scout ahead to find a path. He left coins along the path so that if we always stick to the nearest coin, we'll avoid the traps.
// This canyon seems to interfere with your findNearest glasses!
// You'll need to find the nearest coins on your own.
while(true) {
var coins = hero.findItems();
var coinIndex = 0;
var nearest = null;
var nearestDistance = 9999;
// Loop through all the coins to find the nearest one.
while(coinIndex < coins.length) {
var coin = coins[coinIndex];
coinIndex++;
var distance = hero.distanceTo(coin);
// If this coin's distance is less than the nearestDistance
if (coins >= nearestDistance) {
}
// Set nearest to coin
nearest = coins;
// Set nearestDistance to distance
nearestDistance=distance;
}
// If there's a nearest coin, move to its position. You'll need moveXY so you don't cut corners and hit a trap.
if (coins) {
hero.moveXY(coin.pos.x, coin.pos.y);
}
}
// This field is covered in firetraps. Thankfully we've sent a scout ahead to find a path. He left coins along the path so that if we always stick to the nearest coin, we'll avoid the traps.
// This canyon seems to interfere with your findNearest glasses!
// You'll need to find the nearest coins on your own.
while(true) {
var coins = hero.findItems();
var coinIndex = 0;
var nearest = null;
var nearestDistance = 9999;
// Loop through all the coins to find the nearest one.
while(coinIndex < coins.length) {
var coin = coins[coinIndex];
coinIndex++;
var distance = hero.distanceTo(coin);
// If this coin's distance is less than the nearestDistance
if (coins.distance < nearestDistance) {
nearest = coins;
nearestDistance = distance;
}
// Set nearest to coin
// Set nearestDistance to distance
}
// If there's a nearest coin, move to its position. You'll need moveXY so you don't cut corners and hit a trap.
if (nearest) {
hero.moveXY(nearest.pos.x, nearest.pos.y);
}
}
I believe it should be if (coin.distance < nearestDistance) because “coin” is an object but “coins” is an array. Like wise, your hero doesn’t move because “nearest returns an array” (remember you set nearest to coins?) Make sure you do coins[0] or set nearest to “coin”