Yet Another Keeping Time Thread

I don’t seem to be using the time method properly. My character will destroy the enemy and then pick up the coins…and not stop. I appreciate everyone’s input on this:


while(true) {
    var enemy = hero.findNearestEnemy();
    var coin = hero.findNearestItem();
    // If it's the first 10 seconds, attack.
    if (enemy && hero.time < 10) {
        hero.attack(enemy);
    }
    // Else, if it's the first 35 seconds, collect coins.
    else if (coin && hero.time < 35) {
        hero.moveXY(coin.pos.x, coin.pos.y);
    }
    // After 35 seconds, attack again!
    else {
        var enemy = hero.findNearestEnemy();
        if (enemy && hero.time > 35) {
            hero.cast("summon-burl");
            hero.cast("summon-undead");
            hero.attack(enemy);
        }
    }
}

Well, I watched your replay of the level, and I can’t really tell because of the burls and skeletons.
But if you get rid of them you do attack eventually, it just takes a while and in your burls and skeletons version they just kill the ogres for you.
In my version of this I’ve changed the timings otherwise I destroy the palisades. And I’ve made the second timing 15 instead of 35 so I can kill all the ogres myself and do more damage. :grin:
-Danny

P.S. when making topics please could you always put them in the “level help” category if that’s what it’s about, and maybe make the title as short as possible. Just long enough to tell everyone what it’s about. e.g. “Stuck on Keeping Time”, or as I prefer, just: “Keeping Time” in the “level help” category.
But it’s up to you.

P.P.S @Shurutsue gave a piece of advice about formatting Javascript which I would highly recommend, it was to put “javascript” after the three back ticks, and it formats it accordingly.
e.g. ```javascript (obviously I can’t add the other three or it would format it :smile:)
It turns out like this meaning there are not big red patches on your code caused by non-commented out apostrophes.

while(true) {
    var enemy = hero.findNearestEnemy();
    var coin = hero.findNearestItem();
    // If it's the first 10 seconds, attack.
    if (enemy && hero.time < 10) {
        hero.attack(enemy);
    }
    // Else, if it's the first 35 seconds, collect coins.
    else if (coin && hero.time < 35) {
        hero.moveXY(coin.pos.x, coin.pos.y);
    }
    // After 35 seconds, attack again!
    else {
        var enemy = hero.findNearestEnemy();
        if (enemy && hero.time > 35) {
            hero.cast("summon-burl");
            hero.cast("summon-undead");
            hero.attack(enemy);
        }
    }
}
1 Like
Off topic - avoiding formatting

you can also add \ infront of the ` in order for it not to “format” it, for example

\`\`\`
something
\`\`\`

results in
```
something
```

1 Like