Player not attacking-Sarven Road

loop{ var enemy = this.findNearestEnemy(); if (enemy) { if (this.isReady("cleave") && this.distanceTo(enemy)<10){ this.cleave(enemy); } else if (enemy) { this.attack(enemy); } } else (!enemy){ this.moveXY(58, 55); } }

On the level Sarven road, my character wont attack, he’ll just move straight to 58,55. I don’t know why. I specifically state that if there is an enemy use cleave and if cleave isn’t ready use attack. The lemon just ignores the enemy however and makes no attempt to attack! Whats gone wrong?

Probably there will be a moment when there are no enemies, so you character will then head on their way. You could try move (if you have those boots), otherwise you’ll have to try and move a bit (or wait), and then check for enemies again.

It’s a problem with the understanding of the Move commands

The this.move(target) will be re-evaluated every frame.
The this.moveXY(x,y) will “lock” your hero in the motion until it reaches the (x,y) coordinates.

So when the code starts, no enemies are there : the MoveXY command is executed. Then nothing else is executed until you reach your destination.

I suggest you move step by step with

var x = this.pos.x + 10;
var y = this.pos.y + 10;
this.moveXY(x, y);

in order to move TOWARD and not move TO the top right end corner, and slash enemies along the way. Good luck !

1 Like

Thanks really appreciate you help Vincent

loop {
enemy = this.findNearestEnemy();
distance = this.distanceTo(enemy);
if (enemy) {
if (distance < 10) {
x = this.pos.x + 10;
y = this.pos.y;
this.moveXY(x, y);
I can’t get my player to attack people can someone help?

Well . . . there is no attack command . . . So . . . “this.attack(enemy);”