class flixel.util.FlxMath
Available on all platforms
A class containing a set of math-related functions.
Class Fields
static function bound(Value:Float, Min:Float, Max:Float):Float
Bound a number by a minimum and maximum. Ensures that this number is * no smaller than the minimum, and no larger than the maximum. *
Value | Any number. * |
Min | Any number. * |
Max | Any number. * |
returns | The bounded value of the number. |
static function distanceBetween(SpriteA:FlxSprite, SpriteB:FlxSprite):Int
Find the distance (in pixels, rounded) between two FlxSprites, taking their origin into account * *
SpriteA | The first FlxSprite * |
SpriteB | The second FlxSprite * |
returns | Distance between the sprites in pixels |
static function distanceToMouse(Sprite:FlxSprite):Int
Find the distance (in pixels, rounded) from the object x/y and the mouse x/y * *
Sprite | The FlxSprite to test against * |
returns | The distance between the given sprite and the mouse coordinates |
static function distanceToPoint(Sprite:FlxSprite, Target:FlxPoint):Int
Find the distance (in pixels, rounded) from an FlxSprite * to the given FlxPoint, taking the source origin into account. * *
Sprite | The FlxSprite * |
Target | The FlxPoint * |
returns | Distance in pixels |
static function distanceToTouch(Sprite:FlxSprite, Touch:FlxTouch):Int
Find the distance (in pixels, rounded) from the object x/y and the FlxPoint screen x/y * *
Sprite | The FlxSprite to test against * |
Touch | The FlxTouch to test against * |
returns | The distance between the given sprite and the mouse coordinates |
static function dotProduct(ax:Float, ay:Float, bx:Float, by:Float):Float
Finds the dot product value of two vectors * *
ax | Vector X * |
ay | Vector Y * |
bx | Vector X * |
by | Vector Y * * |
returns | Result of the dot product |
static function getDecimals(Number:Float):Int
Returns the amount of decimals a Float has * *
Number | The floating point number * |
returns | Amount of decimals |
static function getDistance(Point1:FlxPoint, Point2:FlxPoint):Float
Calculate the distance between two points. * *
Point1 | A FlxPoint object referring to the first location. * |
Point2 | A FlxPoint object referring to the second location. * |
returns | The distance between the two points as a floating point Number object. |
static function inBounds(Value:Float, Min:Float, Max:Float):Bool
Checks if number is in defined range. * *
Value | Number to check. * |
Min | Lower bound of range. * |
Max | Higher bound of range. * |
returns | Returns true if Value is in range. |
static function isDistanceToMouseWithin(Sprite:FlxSprite, Distance:Float, ?IncludeEqual:Bool = false):Bool
Check if the distance from the object x/y and the mouse x/y is within a specified number. * A faster algoritm than distanceToMouse because the Math.sqrt() is avoided. * *
Sprite | The FlxSprite to test against * |
Distance | The distance to check * |
IncludeEqual | If set to true, the function will return true if the calcualted distance is equal to the given Distance * |
returns | True if the distance between the sprites is less than the given Distance |
static function isDistanceToPointWithin(Sprite:FlxSprite, Target:FlxPoint, Distance:Float, ?IncludeEqual:Bool = false):Bool
Check if the distance from an FlxSprite to the given * FlxPoint is within a specified number. * A faster algoritm than distanceToPoint because the Math.sqrt() is avoided. * *
Sprite | The FlxSprite * |
Target | The FlxPoint * |
Distance | The distance to check * |
IncludeEqual | If set to true, the function will return true if the calcualted distance is equal to the given Distance * |
returns | True if the distance between the sprites is less than the given Distance |
static function isDistanceToTouchWithin(Sprite:FlxSprite, Touch:FlxTouch, Distance:Float, ?IncludeEqual:Bool = false):Bool
Check if the distance from the object x/y and the FlxPoint screen x/y is within a specified number. * A faster algoritm than distanceToTouch because the Math.sqrt() is avoided. * *
Sprite | The FlxSprite to test against * |
Distance | The distance to check * |
IncludeEqual | If set to true, the function will return true if the calcualted distance is equal to the given Distance * |
returns | True if the distance between the sprites is less than the given Distance |
static function isDistanceWithin(SpriteA:FlxSprite, SpriteB:FlxSprite, Distance:Float, ?IncludeEqual:Bool = false):Bool
Check if the distance between two FlxSprites is within a specified number. * A faster algoritm than distanceBetween because the Math.sqrt() is avoided. * *
SpriteA | The first FlxSprite * |
SpriteB | The second FlxSprite * |
Distance | The distance to check * |
IncludeEqual | If set to true, the function will return true if the calcualted distance is equal to the given Distance * |
returns | True if the distance between the sprites is less than the given Distance |
static function isEven(n:Float):Bool
Returns true if the number given is even. * *
n | The number to check * |
returns | True if the given number is even. False if the given number is odd. |
static function isOdd(n:Float):Bool
Returns true if the number given is odd. * *
n | The number to check * |
returns | True if the given number is odd. False if the given number is even. |
static function lerp(Min:Float, Max:Float, Ratio:Float):Float
Returns linear interpolated value between Max and Min numbers * *
Min | Lower bound. * |
Max | Higher bound. * |
Ratio | Defines which number is closer to desired value. * |
returns | Interpolated number. |
static function maxAdd(value:Int, amount:Int, max:Int, ?min:Int = 0):Int
Adds the given amount to the value, but never lets the value * go over the specified maximum or under the specified minimum. * *
value | The value to add the amount to * |
amount | The amount to add to the value * |
max | The maximum the value is allowed to be * |
min | The minimum the value is allowed to be * |
returns | The new value |
static function mouseInFlxRect(useWorldCoords:Bool, rect:FlxRect):Bool
Returns true if the mouse world x/y coordinate are within the given rectangular block * *
useWorldCoords | If true the world x/y coordinates of the mouse will be used, otherwise screen x/y * |
rect | The FlxRect to test within. If this is null for any reason this function always returns true. * * |
returns | true if mouse is within the FlxRect, otherwise false |
static function numericComparison(num1:Float, num2:Float):Int
Compare two numbers. * *
num1 | The first number * |
num2 | The second number * |
returns | -1 if num1 is smaller, 1 if num2 is bigger, 0 if they are equal |
static function pointInCoordinates(pointX:Float, pointY:Float, rectX:Float, rectY:Float, rectWidth:Float, rectHeight:Float):Bool
Returns true if the given x/y coordinate is within the given rectangular block * *
pointX | The X value to test * |
pointY | The Y value to test * |
rectX | The X value of the region to test within * |
rectY | The Y value of the region to test within * |
rectWidth | The width of the region to test within * |
rectHeight | The height of the region to test within * * |
returns | true if pointX/pointY is within the region, otherwise false |
static function pointInFlxRect(pointX:Float, pointY:Float, rect:FlxRect):Bool
Returns true if the given x/y coordinate is within the given rectangular block * *
pointX | The X value to test * |
pointY | The Y value to test * |
rect | The FlxRect to test within * |
returns | true if pointX/pointY is within the FlxRect, otherwise false |
static function pointInRectangle(pointX:Float, pointY:Float, rect:Rectangle):Bool
Returns true if the given x/y coordinate is within the Rectangle * *
pointX | The X value to test * |
pointY | The Y value to test * |
rect | The Rectangle to test within * |
returns | true if pointX/pointY is within the Rectangle, otherwise false |
static function roundDecimal(Value:Float, Precision:Int):Float
Round a decimal number to have reduced precision (less decimal numbers). * Ex: roundDecimal(1.2485, 2) -> 1.25 * *
Value | Any number. * |
Precision | Number of decimal points to leave in float. Should be a positive number * |
returns | The rounded value of that number. |
static function sameSign(f1:Float, f2:Float):Bool
Checks if two numbers have the same sign (using signOf()).
static function vectorLength(dx:Float, dy:Float):Float
Finds the length of the given vector * *
dx | * |
dy | * * |
returns | The length |
static function wrapValue(value:Int, amount:Int, max:Int):Int
Adds value to amount and ensures that the result always stays between 0 and max, by wrapping the value around. * Values must be positive integers, and are passed through Math.abs * *
value | The value to add the amount to * |
amount | The amount to add to the value * |
max | The maximum the value is allowed to be * |
returns | The wrapped value |