all utility functions are static, therefore called like this:

 

MbUtil.nameOfFunction(...)

MbUtil

filters&colors | text | number formatting | arrays | sliders&pictures | math

Filters and colors

static applyEasyFilter(obj:DisplayObject, channels:Array, offsets:Array):void {

"channels are RGBA multipliers, offsets are RGBA baselines

static hexToCol(h:int):String

use to debug colors

static lighten(col:uint, inc:uint):uint

static darken(col:uint, inc:uint):uint {

darken / lighten a color: e.g. lighten (0xffaa99, 0x111111) --> 0xee9988

static makeRect(bgCol, outlineCol, thick, w, h):Shape

the easy way to draw a rect -- still need to addChild, e.g.

addChild(drawRect(0xff0000, 0x000000, 2, 20, 20));

Text

static cloneTextField(from:TextField, newText:String):TextField

creates a text field with the same properties as the original. does NOT addChild.

static createText(text:String, tf:TextFormat, x, y, width): TextField

creates a textfield, does NOT add to stage

defaults; multiline wordwrap, not selectable

static putText(txt:String, x,y:Number, f:String="appDirections"):TextField

same as createText, but accepts any string from MbFormat as formatting

"appTitle", "appDirections", "hint" , "grAxes", "grTitle", "testing"

static centerText(txt:String, x,y:Number, f_:String=""):TextField

like putText, but returns centered text

static format(size_:int, color_:uint, bold_:Boolean):TextFormat

use to create a quick text format

static rightAlign():TextFormat

creates a right-aligned TextFormat

static wrapTextInClass(tag:String, cls:String, txt:String):String

use in html text

static makeCssTextField(w:int, h:int, css:StyleSheet):TextField

creates a textfield with a css stylesheet attached

Number Formatting

static numberToString(n:Number):String

static stringToNumber(s:String):Number

formats numbers with commas woo hoo!

static forceNumber(txt:String):Number

finds a number embedded in text

public static function roundDecimal(num:Number, places:int):Number

works with pos and neg #s

public static function sigdig(num:Number, sd:int):String

not tested with neg number

Array handling

static shuffleArray(arr:Array)

static chunkArray(arr:Array):Array

sorts array and counts number of times each entry appears

returns 2 arrays -- list of entries, list of times each entry appears

static uniqueArray(arr:Array):Array

removes duplicates

static sumArray(arr:Array):Number

adds array

static newArray(n:int, init:Number):Array

clones array

static linAddArray(a:Number, arrA:Array, b:Number, arrB:Array):Array

add 2 arrays, using multipliers a and b (e.g., aA + bB)

static linAddArrayYs(a:Number, arrA:Array, b:Number, arrB:Array):Array

add y-values of 2 arrays, using multipliers a and b (e.g., aA + bB)

static traceArray(arr:Array)

traces each item in array

Sliders, Pictures

static setupSlider(sliName:Slider, min:Number, max:Number, val:Number, handler:Function)

not sure if this works

static setupPicture(url:String, handlerFunction):Loader

puts a picture on the stage, e.g.,

picLoader = MbUtil.setupPicture("bird.jpg", handleImage);

function handleImage(event:Event):void {
while (picCont.numChildren>0)
picCont.removeChildAt(0);
picCont.addChild(picLoader.content);
}

Math

static randInt(from:int, to:int):int

static randNumber(from:Number, to:Number):Number

static randNormal(mn:Number, sd:Number):Number

static log10(n:Number) : Number

static approx(n1:Number, n2:Number, perc:Number):Boolean

determines whether n1 and n2 are with perc% of each other

static btw(n1:Number, bNum:Number, n2:Number):Boolean

determines with n1 < bNum < n2

static min(num_array:Array):Number

static max(num_array:Array):Number