I'm working on a function that takes the stage mouse coordinates in Flash AS3 and returns tile in a hex-grid below the cursor. I want the function to return NaN or undefined if the cursor is not on a tile and if it is, the integer index of that tile in an object array.
public function getCursorTile(mX:Number,mY:Number):uint
{
var tileIndex:uint = new uint(NaN);
trace(tileIndex);
for (i=0; i<tileArray.length; i++)
{
if (tileArray[i].hitTestPoint(mX,mY,true))
{
tileArray[i].tileBorder.gotoAndStop(1);
tileIndex = i;
}
}
return tileIndex;
}
I've been using uint for most integer variables, perhaps this data type doesn't support NaN or undefined? This code traces 0 after tileIndex is defined. I'm using hitTest because the hexgrid isn't square and is randomly generated. Tile 0 is the first tile and returning 0 when the cursor is off the hex-map is going to cause problems.
NaN is of type Number. You can't cast it meaningfully into a uint.
If you really must return NaN, change your function signature to return :Number
, but understand that you might take a slight performance penalty for it if that function is on your critical path.
Some C-style trickery could be used here: If meaningful tile indexes are always positive, and you feel confident you won't ever end up with more than 2 billion tiles, you could return -1 to mean "not a tile". You'd need to change :uint
into :int
, and be careful about implicit conversions between the two types throughout your code.