This is because move(position)
is very different from moveXY(x, y)
. Both are extremely useful, but have different requirements to make work.
##moveXY(x, y)
This is a blocking statement, which means that it will continue to move and do nothing else until that position is reached or it is determined that it cannot be reached. This is great for situation-s that are slow and/or semi-static. It is also great when you don’t want to be interrupted from your goal of getting to a particular position. Finally, if the path to the point isn’t too complex, then moveXY()
can often excel.
##move(position)
This is a nonblocking statement, meaning you can do other things while executing this command. When you use this, your hero will take one “step” toward the position indicated. This means that for it to be useful, it must be used in a loop. The nature of this function often is confusing for many at first because the requirements for destination based actions are different.
This function is most useful for fast changing situations, particularly when you want your hero to perform actions along the way. Here are a couple of examples:
Coin Collection
Often, in coin collection levels, coins are being dropped on the field as you are moving to collect others. If your hero is moving to a coin that is far across the field, but a closer coin appears while in movement, moveXY()
will not allow you to change directions. move()
, however, will allow you to redirect your hero to the newer, closer coin.
var nearestCoin;
loop {
// Will check every loop for the nearest coin...
nearestCoin = this.findNearest(this.findItems());
// Will continue to move to the coin until a better nearest appears...
if (nearestCoin) {
this.move(nearestCoin.pos);
}
}
Attack to Position
Since you can’t attack while moving with moveXY()
, this is pretty self-evident. With move()
you can move along a path and attack whatever comes into range, no longer subjecting your hero to unnecessary hits.
loop {
destination = // Some process...
nearest = this.findNearest(this.findEnemies());
// Kill anything that might block my path
if (nearest && this.distanceTo(nearest) < this.attackRange) {
this.attack(nearest);
}
// Otherwise, continue moving...
else if (destination) {
this.move(destination);
}
}
These are just a few simple examples, but the idea remains the same… If your destination is constant, just store it in a variable and keep moving toward it. If the destination is going to change often then you have to keep checking where the best destination is and move toward that instead.