I am developing a strategy board war game. When I click a troop sprite and then click on the map, it should set the animation frame depending on the distance to travel. For instance, if I click 3 tiles away, it should show an arrow which length covers 3 tiles. If clicked on a neighbour tile, just 1 tile size arrow. In the arrow sprite, first frame shows 1 tile arrow, second frame, 2 tiles arrow. And so on.
The events for that are quite simple. The issue is I want to keep things proportional, so into the parameters dialog for the frame to show up, I want to calculate the distance in tiles between the target tile and the one the troop is onto.
To achieve that, I use Pithagoras theorem. Both sides of the triangle are Touch.X-Troop.X and Touch.Y-Troop.Y.
So, distance = hypotenuse, thus:
(√ ( (Touch.X - Troop.X)² + (Touch.Y - Troop.Y)²))/"tile size"
The formula above is not accurate enough, for it calculates from where I touch, not the center of the tile, where the troop will land. Yet not by mistake. Just for sake of simplicity. It would be Tilemap.TileToPositionX(Tilemap.PositionToTileX(Touch.X)) instead of just Touch.X. Troop needn't recalculating, because it will always be at the center of the tile, due to the previous movement having already calculated that.
So, the entire formula looks like this:
(√ ( Tilemap.TileToPositionX(Tilemap.PositionToTileX(Touch.X)) - Jugador.X)² + (Tilemap.TileToPositionX(Tilemap.PositionToTileX(Touch.X)) - Jugador.Y)²))/64
That should be into the parameter box for the value of the frame to be called. That way, 1 tile away calls 1st frame, which is 1 tile arrow,...
But I am having a hard time writing down the expression, for I have no experience on programming. The trouble is not finding the right expressions, but the right syntax. Maybe I am missing some symbol or adding some other that shouldn't be there.