Munchkin Swarm code problems

thank you .

I got it

1 Like

That is the code

[redacted]

1 Like

I have also an error with this level.

The method ā€œfindNearestEnemyā€ doesnā€™t show up and also the object enemy is null. Iā€™m using JavaScript. My hero wears the wooden glasses, but in the lower right these methods are not displayed, so unusable.

1 Like

You might have a better item than the early ones that provide findNearestEnemy.

in which case youā€™ll need to do something like this.

self.findNearest(self.findEnemies())

as for the null enemy object, yes it will be null if there is not enemy at that moment. be sure to safeguard against this by putting all code that accesses the enemy object inside an

if enemy:
2 Likes

Been at if for some time and I canā€™t seem to figure it out.

var enemy = this.findNearestEnemy();
var distance = this.distanceTo(enemy);

loop {
if (distance < 10) {
this.cleave(enemy);
}
else {
this.attack(ā€œChestā€);
}
}

The thing is that my Hero wont stop attacking the ā€œChestā€ even if the distance is < 10.
any tips for this problem.

I dont know why but my ā€œelseā€ code wont work.

@LITE You need to find the enemy and calculate the distance within the loop. Having it outside the loop means itā€™s only done once at the start, so your character will never find the enemy when your code is looping.

1 Like

oooh got it. thanks!

loop: # Check the distance to the nearest enemy. # If it comes closer than 10 meters, cleave it! # Else, attack the "Chest" by name. if self.distanceTo(self.findNearestEnemy()):10 self.cleave(self.findNearestEnemy()) self.attack("Chest")

Merged Doublepost

this is my code can anyone help me with this please??? :sob:

Did you get this in the end? If posting code itā€™s best to format your code so we can see what is actually there (see the FAQ). Iā€™m guessing this is Python, and assuming your indentation is ok (canā€™t really see from whats there), you might want to check the comparison used in your if - should be a < I think

thanks I got it now thanks for the help

On this topic . . .
The help video says to add .pos when you declare distanceto because ā€œwe used it in the previous levelā€. Um, no we DIDNā€™T. I had never seen it before now, and that isnā€™t in the code examples.

Iā€™m wondering what happened there . . .

I believe I am using the exact code from the help video and yet it says the last line is not a function.

loop:
enemy = self.findNearestEnemy()
enemyDistance = self.distanceTo(enemy)
if self.isReady(ā€œcleaveā€):
if enemyDistance< 10:
self.cleave(enemy)
else:
self.attack(ā€œChestā€)
else:
self.attack(ā€œChestā€)

OK, I HAD TO SWITCH TO THE LONG SWORD AGAIN. i KEEP FORGETTING i NEED THAT TO CLEAVE. ON MANY LEVELS I HAVE HAD DIFFERENT KINDS OF CODE ERRORS WHICH I HAVE FOUND IN THE END TO BE A RESULT OF NOT HAVING THE RIGHT SWORD, BOOTS OR HERO.

Please do not post correct code! It is not allowed according to the Discourse FAQ @Conor_Extreme can you edit it out?

1 Like

Here is what i have and nothing worksā€¦
while True:

enemy = self.findNearestEnemy()
distance = hero.distanceTo(enemy)
hero.say(10)

enemy = hero.findNearestEnemy
if enemy < 10:
    ready = hero.isReady("cleave")
    hero.cleave(enemy)

else:
    hero.attack("Chest")
    hero.attack("Chest")

I recommend you press the ā€œRestartā€ button at the top right corner of the screen and read the instructions carefully.