Pet engineer, code doesn't work


#1

Everytime the enemy comes, my pet doesn’t move at all.
This is my code:

// Move your pet to the left or right button as needed.

function onHear(event) {
    // Find the guards to listen to.
    var archer = pet.findNearestByType("archer");
    var soldier = pet.findNearestByType("soldier");
    // If event.speaker is the archer:
    if (event.speaker && "hear" == "archer") {
        hero.moveXY(32, 30);
    }
        // Move to the left button.
        
    // If event.speaker is the soldier:
    if (event.speaker && "hear" == "soldier") {
        hero.moveXY(48, 30);
    }
        // Move to the right button.
        
}

pet.on("hear", onHear);

// You don't have to change the code below.
// Your hero should protect the bottom right passage.
while(true) {
    var enemy = hero.findNearestEnemy();
    if (enemy) {
        hero.attack(enemy);
    }
}


#2

Are you moving the hero or the pet in the pet onHear function?


#3

going to do that
(post needs 20 characters)


#4

ok, I changed it, but the pet still won’t move. Is there anything wrong with the hear thing?


#5

Place this into your code

hero.say(event.speaker);

after the var soldier = pet.findNearestByType("soldier"); and ask yourself how the if statement should be created.


#6

I will. I’ll reply after.