Hey! So I can’t finish the Munchkin Swarm level because my character walks off the screen whenever I put this.isReady(“cleave”) into the arguments for my if statement. This is my code:
loop {
var enemy = this.findNearestEnemy();
var distance = this.distanceTo(enemy);
if (enemy < 10, this.isReady(“cleave”)) {
this.cleave(enemy);
}
else {
this.attack(“Chest”);
}
}
The reason he walks off the screen is because the nearest enemy at the beginning of the level is off the screen,so he goes towards the enemy to cleave him.
2.If Condition
Your if condtion is wrong.And enemy<10 dosent make sense, I think you probably meant distance<10
if (distance < 10 && this.isReady("cleave")) {
this.cleave();
}
Putting the comma operator in your if condition has a totally different meaning to the if condition.If you like I could explain how the comma operator works
UPDATE : Comma Operator
The comma operator evaluates all the expressions and returns the last expression
var i = (1,2,3,4); //The value of i will be 4 because 4 is the last expression
var j = (1,5,3,1); //Similarly the value of j will be 1 s
if(true,false,true,false){
// This if block wont get executed.Guess why ?
// Because the comma operator returns the last expression which is false.
// So the first 3 "true,false,true" dosent affect the if condition in anyway
}
Hope this made sense.Feel free to ask any question.
ah! Thank you! That makes a ton of sense
I guess I was confused since it didn’t return any errors
yeah, I would love to know how it works in Javascript!
Ah, good point. The chest isn’t a collectable item; it’s actually sort of kind of an enemy, but for this level we wanted enemies to just be ogres. The sample code includes this.attack("Chest") as a hint.
Ok. Agree that the chest in the enemy array is too complexe at this step.
Don’t know if it’s possible to remove the useless feature findNearestItem…In fact, it’s maybe not a problem for beginner who will recopy the “Chest” of the previous level, but maybe just for people who are used to use find methods
You should be getting the glasses two levels after that so that you can use the distanceTo and shortly thereafter use the findNearestItem to start picking up coins. I don’t know if there’s some bug or what that is getting so many advanced users playing the levels in some other order!
Are you sure ? I get the wooden glasses for “Peasant protection”, just before “Munchkin Swarm”. There we need the “distance to” sort.
So with the wooden glasses, we get “findNearestItem”.
And the wooden glasses are also needed to use “Distance to” in this level.