Bookkeeper help JS


#1

my character will not attack the ogres. can someone help me with this

while (true) {
enemies = this.findEnemies;
nearest_enemy = this.findNearest(enemies);
if (enemies && nearest_enemy){
this.attack(nearest_enemy);
}
if (nearest_enemy && nearest_enemy.health <=0){
defeated=defeated+1;
}
if (this.now() > 15){
this.moveXY(59, 33);
this.say( defeated );
break;
}
}
while (true){
items = this.findItems();
nearest_item = this.findNearest(items);
if (nearest_item){
this.move(nearest_item);
}
if (this.now()>30){
this.moveXY(58, 33);
this.say(self.gold);
defeated=0;
break;
}
}
while (true){
enemies = this.findEnemies();
nearest_enemy = this.findNearest(enemies);
if (enemies && nearest){
this.attack(nearest_enemy);
}
if (nearest && nearest.health <=0){
defeated=defeated+1;
}
if (this.now() > 45){
this.moveXY(59, 33);
this.say( defeated );
}
break;
}


#2

The first thing I saw when I looked at your code was that you never introduced your variables with the prefix var, (oh and by the way use that little </> sign in your toolbar to format your code). So to introduce enemies you type var enemies = this.findEnemies();. You never even defined defeated, and when you do you might what to define it outside of a loop so it isn’t reset each time the code runs. Also when you are moving to the coin position you need to qualify your can’t just write this.move(nearest_item);, because nearest_item is an object and you need to move to it’s .pos.