Skip to the content.
Function Arguments Description Code
setAngleSpeed() (angle:Float, speed:Float) Applys a set speed in a set direction instead of using X and Y
Codeblock
function setAngleSpeed(angle:Float, speed:Float) {
  var rads:Float = -angle * (Math.PI / 180);
  var m_X:Float = Math.cos(rads);
  var m_Y:Float = Math.sin(rads);
  self.setXSpeed(m_X * speed);
  self.setYSpeed(m_Y * speed);
}
weightedRandomChoice() (choices: Array<Dynamic>, weights: Array<Number>) Returns a weighted random choice from an array based on supplied weights.
Codeblock
function weightedRandomChoice(choices: Array<Dynamic>, weights: Array<Number>)
{
    //Error checking because I'm just responsible like that
    if (weights.length != choices.length){
        throw "Weights array is a different length than the choices array. Please ensure both argument arrays are the same length before calling the function.";
    }

    //Generate a random number between 0 and the sum of the weights
    var weightSum = 0;
    Engine.forEach(weights, function(weight, i) {
        //More error checking because I'm really really responsible I promise
        if(weight < 0) throw "weight " + weight + " at index " + i + " is negative. Please ensure all supplied weights are non-negative.";

        weightSum += weight; return true;
    }, []);
    var rNum = Random.getFloat(0, weightSum);

    //Run through the weights array until the sum of weights up to that point is less than or equal to the generated random number
    var choice = -1;
    weightSum = 0;
    Engine.forEach(weights, function(weight, index) {
        weightSum += weight;
        if(rNum <= weightSum) {
            choice = choices[index];
            return false;
        }
        return true;
    }, []);

    return choice;
}