Help with Munchkin Swarm


#1

I need help with Munchkin Swarm. Here is my code. Where am i going wrong? My hero is getting killed and she does not seem to be attacking the chest either. Thank you.

    var enemy = this.findNearestItem();
       if (enemy){
    var distance = this.distanceTo(enemy);
      if (distance < 10){
          if (this.isReady("power-up")){
              this.powerUp();
               } else {
                   this.attack(Chest);
               }
      }
      }  
    // Check the distance to the nearest enemy.
    
    // If it comes closer than 10 meters, cleave it!
    
    // Else, attack the "Chest" by name.
    
}

#2

i have also added a shield action. but my hero does not seem to be doing anything when i run my code. Please help. Here is my code with the Shield action. Thank you.

loop {
    var enemy = this.findNearestItem();
       if (enemy){
    var distance = this.distanceTo(enemy);
      if (distance < 10){
          if (this.isReady("power-up")){
              this.powerUp();
               } else {
                  this.shield();
               }
               if(distance > 10){
                   this.attack(Chest);
               }
               
      }
      }  
    // Check the distance to the nearest enemy.
    
    // If it comes closer than 10 meters, cleave it!
    
    // Else, attack the "Chest" by name.
    
}

#3

Be sure to properly format your code as you type it, it makes it easier to spot any errors that may happen, for Javascript, you want to indent a line for every set of brackets it is in:

loop {
    var enemy = this.findNearestEnemy();
    if(enemy) {
        // do something
    } else {
        var item = this.findNearestItem();
        if(item.pos.x > 10) {
            // do something else.
        }
    }
}

Take a moment to format your code so that things are intended based on how many brackets they are inside, and you might see the problem.


#4

I just want to say the thing that is most apparent. You defined enemy as item.


#5

You need to attack the chest like this

this.attack("Chest");

and I simply attacked the chest and ignored the enemies.
Good Luck! :wink:


#6

Sorry guys. i was MIA for a bit. Thanks a lot. I added the “attack chest” function write at the beginning and it worked. Thanks.