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);
}
}
I’m having the same problem with my code. I tried hero.say(event.speaker), but my pet is just not responding to my commands. I looked at my code and I thought that it seemed fine, but the onHear code stops after defining soldier.
def onHear(event):
# Find the guards to listen to.
archer = pet.findNearestByType(“archer”)
soldier = pet.findNearestByType(“soldier”)
hero.say(event.speaker)
# If event.speaker is the archer:
if event.speaker == “archer”:
# Move to the left button.
pet.moveXY(32, 30)
# If event.speaker is the soldier:
if event.speaker == “soldier”:
# Move to the right button.
pet.moveXY(48, 30)
pet.on(“hear”, onHear)
while True:
enemy = hero.findNearestEnemy()
if enemy:
hero.attack(enemy)`
Mod edit: Since the code originally posted is so close to an actual solution, the code section has been modified to show only the problem area. This will ensure that others will not be able to copy it and pass the level without actually learning the lesson.
# If event.speaker is the `archer`:
if event.speaker == "archer":
# Move to the left button.
pet.moveXY(32, 30)
# If event.speaker is the `soldier`:
if event.speaker == "soldier":
# Move to the right button.
pet.moveXY(48, 30)
My understanding is that putting a word inside quotation marks (either ’ ’ or " ") makes it into a string. Without the quotation marks it might be a variable (assuming you’ve defined it as something). So “archer” and archer will be interpreted differently.
thank you so much i just deleted the " and it still wouldn’t work and then i realised my glasses is see through
edit: nvm i pressed submit now its not working?
def onHear(event):
# Find the guards to listen to.
archer = pet.findNearestByType("archer")
soldier = pet.findNearestByType("soldier")
hero.say(event.speaker)
# If event.speaker is the `archer`:
if event.speaker == archer:
# Move to the left button.
pet.moveXY(32, 30)
# If event.speaker is the `soldier`:
if event.speaker == soldier:
# Move to the right button.
pet.moveXY(48, 30)
pet.on("hear", onHear)
# You don't have to change the code below.
# Your hero should protect the bottom right passage.
while True:
enemy = hero.findNearestEnemy()
if enemy:
hero.attack(enemy)
the pet goes the right artillery but it doesn’t work and then he goes to the left and then back to the right but when he fires it at the right it’s too late and two brawlers kill the humans