{SOLVED}Long'Range Division - the second one


#1

I really don’t understand what is wrong with this code… My hero doesn’t detect the second enemy to kill him. Is this the code? Or something is special in the second enemy abilities? Help me…

// Destroy the mines!
// Use say to call out the range to the mines.
// Use division to calculate the range.

var enemy = hero.findNearestEnemy();
var distanceToEnemy = hero.distanceTo(enemy);

// Say first Range: distanceToEnemy divided by 3
hero.say(distanceToEnemy / 3);
hero.say(“Fire!”);

// Say second range: distanceToEnemy divided by 1.5
hero.say(distanceToEnemy / 1.5);
hero.say(“Fire!”);

// Say these things for motivation. Really. Trust us.
hero.say(“Woo hoo!”);
hero.say(“Here we go!”);
hero.say(“Charge!!”);

// Now, use a while-true loop to attack the enemies.
while(true) {
if (enemy) {
hero.attack(enemy);
}
}


#2

It’s because you’ve only defined enemy once: var enemy = hero.findNearestEnemy(); outside the while(true) { loop, so inside the while(true) { loop it’s only attacking the one you defined once, therefore you’ve got to define enemy inside the while(true) { loop. That’s all.
Hope this helps! :grin:


#3

Thx Deadpool. I thought about it, but, don’t know why, i didn’t test it… :expressionless: How stupid ! Thx.


#4

My pleasure, please could you also put a [SOLVED] in the title description to stop anyone thinking you still had a problem. Also in the future could you use the pre-formatted code button to format your code properly, It’s next to the quote button on the taskbar and it will come up with the text: “type or paste code here” paste your code directly from the game, It would look like this:

// Destroy the mines!
// Use say to call out the range to the mines.
// Use division to calculate the range.

var enemy = hero.findNearestEnemy();
var distanceToEnemy = hero.distanceTo(enemy);

// Say first Range: distanceToEnemy divided by 3
hero.say(distanceToEnemy / 3);
hero.say(“Fire!”);

// Say second range: distanceToEnemy divided by 1.5
hero.say(distanceToEnemy / 1.5);
hero.say(“Fire!”);

// Say these things for motivation. Really. Trust us.
hero.say(“Woo hoo!”);
hero.say(“Here we go!”);
hero.say(“Charge!!”);

// Now, use a while-true loop to attack the enemies.
while(true) {
    if (enemy) {
        hero.attack(enemy);
    }
}

Thanks.