Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

Sarven Treasure Javascript

Hello! Can anybody, pls, help me with this code (I believe this is an easy issue for you)?

The problem is that my hero doesn’t pick up gold after killing ogres and I don’t understand why…

while (true) {
    var enemies = hero.findEnemies();
    var enemyIndex = 0;
    var coins = hero.findNearestItem();
    var coinIndex = 0;
    while (enemyIndex < enemies.length) {
        var enemy = enemies[enemyIndex];
        if (enemy) {
            hero.attack(enemy);
        }
        enemyIndex += 1;
    } 
    if (!enemies && coinIndex < coins.length) {
        var coin = coins[coinIndex];
        if (coin) {
            hero.moveXY(coin.pos.x, coin.pos.y);
        }
        coinIndex += 1;
    }
}

Try to remove the text it shows

Tried with !enemies && and without this line.

Doesn’t work either way.

The hero just stands still in the middle.

What is your code without that?

while (true) {
    var enemies = hero.findEnemies();
    var enemyIndex = 0;
    var coins = hero.findNearestItem();
    var coinIndex = 0;
    while (enemyIndex < enemies.length) {
        var enemy = enemies[enemyIndex];
        if (enemy) {
            hero.attack(enemy);
        }
        enemyIndex += 1;
    } 
    if (coinIndex < coins.length) {
        var coin = coins[coinIndex];
        if (coin) {
            hero.moveXY(coin.pos.x, coin.pos.y);
        }
        coinIndex += 1;
    }
}

Take a closer look at this:

    if (!enemies && coinIndex < coins.length) {
        var coin = coins[coinIndex];
        if (coin) {
            hero.moveXY(coin.pos.x, coin.pos.y);
        }
        coinIndex += 1;
    }

Because this is an ‘if block’, it runs only once; because it is a member of the ‘while true’ loop, it will run once per iteration. However, you also reset ‘coinIndex’ to 0…coinIndex is always going to === 0

I am having problems too but i am on Sarven treasure 4

while True:
    enemys = hero.findEnemies()
    items = hero.findItems()
    item = hero.findNearest(items)
    enemy = hero.findNearest(enemys)
    if (enemy and hero.distanceTo(enemy) < 15):
        hero.attack(enemy)
    elif (item):
        if (hero.isReady('jump')):
            hero.jumpTo(item.pos)
        else:
            hero.move(item.pos)
        

This is my js code:

while (true) {
    enemys = hero.findEnemies();
    items = hero.findItems();
    item = hero.findNearest(items);
    enemy = hero.findNearest(enemys);
    if (enemy and hero.distanceTo(enemy) < 15) {
        hero.attack(enemy)
   } else (item) {
        if (hero.isReady("jump")) {
            hero.jumpTo(item.pos)
         } else {
            hero.move(item.pos)}
       }
}
    

turned it into while-loop

while (!enemies && coinIndex < coins.length) {
        var coin = coins[coinIndex];
        if (coin) {
            hero.moveXY(coin.pos.x, coin.pos.y);
        }

And nothing happened.

I got a feeling that there’s an infinite loop with killing ogres and my character’s not breaking from it.

if there is an infinite loop the level will alert you