[SOLVED] Hit and freeze javascript help

function inAttackRange(enemy) {
var distance = hero.distanceTo(enemy);
// Almost all swords have attack range of 3.
if (distance <= 3) {
return true;
} else {
return false;
}
}

// Attack ogres only when they’re within reach.
while (true) {
// Find the nearest enemy and store it in a variable.
var enemy = hero.findNearestEnemy();

// Call inAttackRange(enemy), with the enemy as the argument

// and save the result in the variable canAttack.
var canAttack = inAttackRange(enemy);
// If the result stored in canAttack is true, then attack!
if (true) {
    hero.attack(enemy);
    }

}
what am i doing wrong i keep failing. after i kill 5 i die

2 Likes

You need to check if (canAttack == true) { - or at least that’s what I think the format is…I wouldn’t know since I only know Python :sweat_smile:

5 Likes

Nailed it :nut_and_bolt:

Another way to write it is like this:

if (canAttack){

In JS, this will check if canAttack is true, not empty, or not null / none

4 Likes

I tried it, and I keep taking damage, because the character is moving toward the munchkins. Is there a way to fix it?
Here’s my code(Javascript):
// You are trapped. Don’t move, it’ll be painful.

// This function checks if the enemy is in your attack range.
function inAttackRange(enemy) {
var distance = hero.distanceTo(enemy);
// Almost all swords have attack range of 3.
if (distance <= 3) {
return true;
} else {
return false;
}
}

// Attack ogres only when they’re within reach.
while (true) {
// Find the nearest enemy and store it in a variable.
var enemy = hero.findNearestEnemy();
// Call inAttackRange(enemy), with the enemy as the argument
var canAttack = inAttackRange(enemy);
// and save the result in the variable canAttack.
if(canAttack){
}
// If the result stored in canAttack is true, then attack!
hero.attack(enemy);
}

Please properly format your code…[Essentials] How To Post/Format Your Code Correctly

What weapon are you using? If the enemy is out of attack range, the hero will attempt to move towards it, so that it comes in range.

Never mind, I managed to beat the level.

your attack was not in the if statement.

Hi, please don’t revive dead topics unless you have a similar problem. YT_DropTheBeat has not been active and they won’t see your post.
Thanks,
Lydia

# You are trapped. Don't move, it'll be painful.

# This function checks if the enemy is in your attack range.
def inAttackRange(enemy):
    distance = hero.distanceTo(enemy)
    # Almost all swords have attack range of 3.
    if distance <= 3:
        return True
    else:
        return False

# Attack ogres only when they're within reach.
while True:
    # Find the nearest enemy and store it in a variable.
    enemy = hero.findNearestEnemy()
    
    # Call inAttackRange(enemy), with the enemy as the argument
    # and save the result in the variable canAttack.
    inAttackRange(enemy)
    # If the result stored in canAttack is True, then attack!
    if 'canAttack' 'true':
        hero.attack(enemy)
        hero.attack(enemy)
        pass

my hero keeps dying can you please help me

Hi @floridamanog, welcome to the forum! :tada:
There are two problems here:

The comments say:

You’ve done the first bit right, but you needed to save the result in a variable called canAttack.
The inAttackRange(enemy) function returns either True or False. You need to put that value in a variable. inAttackRange returns a value like hero.findNearestEnemy() returns a value. So you need to put inAttackRange on the right of an equals sign like you did here:

But instead of enemy you would have canAttack.
Then, in your if statement, you would use the canAttack variable like you have, but without the quotes ‘’. That makes it a string: a message with doesn’t have any value. To check if something is stored in a variable you use ==. Like if enemy.type == “burl”:, but you need to check if canAttack equals True. Note there are no quotes around True, and it has a capital T.
I hope this helps,
Danny

Thank you, i needed to take away a attack and replace it with a cleave

and it worked so well