Hero never stops "moving"


#1

Keep in mind that i’m still learning some stuff about codding, so if there’s some obvious answer i’m not seeing, please let me know :P.

So in dungeon arena, i’m trying to get my Hero to move to a specific position, and then do something once he gets there, but the problem is, he technically never “gets” there.

i’m using the command “this.move({x:,y:});” to get him to move somewhere, but once he reaches that position, he just plays the walking animation endlessly.

i have a statement that says “if(this.pos == {x:,y:}){dostuff}” but nothing happens when he gets there, he just plays an endless walking animation at the position.

what am i missing?


#2

Describe the endless walking animation? In what direction is he walking? Towards something? Straight forward? Moving in place?

As far as it goes, I think there might be an infinite loop somewhere, or perhaps a condition that you make him walk on that is always true. I assume your code looks something like:

if(conditionToMove) {
    this.move({x:30, y:30});
    return;
}

You may be forgetting to alter the condition to be false so that the next iteration of your code detects that it is false and appropriately skips it. If you never change the condition, you’ll always move there, even if he’s already there.

If not, providing more details would help. It would be easiest if we could see the entire code to see what’s happening (maybe via PM if you don’t want to post it).


#3

I’m doing something like this.

if(this.health < 100){
this.move({x:30,y:30});
if(this.pos == {x:30,y:30}){
this.shield();
}
}

not exactly like this, just an example.

If the hero’s health gets to this point, they will move the the position, but once they reach the position (30,30), the hero will continue to play the moving animation in the same direction they were going in, as if they were still moving to the coordinates.


#4

Maybe try something like this:

if(this.health < 100){
  if(this.pos == {x:30,y:30}){
    this.shield();
  } else {
    this.move({x:30,y:30});
  }
}

#5

The problem here is that JavaScript == will not compare two different pos objects like that, even if the x and y are the same. (In fact, this.pos is a CodeCombat Vector and not a plain {x:, y:} Object.) Also, even if it could, this.pos will probably not exactly be at {x: 30, y: 30}, but rather just very close to it (like {x: 30.0052593625, y: 29.926367377}), since x and y are both floating point numbers.

Try using something like this:

if (this.distance({x: 30, y: 30}) < 1)
    this.shield();

#6

Alright, i tested that out, and it works great!
Thanks!


#7

Additional tip: I would recommend you think about how you want to deal with push from friendly or enemy troops. If you want to stay shielded, you may want to increase the distanceTo test beyond 1…

Also, sometimes running away beats shield. Some folks refer to the ancient art of running around in circles, screaming while trying to stay alive as kiting. If you’re fast enough, you may also be able to cast while doing the circles…