Sometime is more useful to have a random number in a different range, let’s say between 15 and 45, instead of 0 and 1.
public static function randNumber(max:Number = 1,min:Number = 0):Number{ returnMath.random()*(max-min)+min; }
Random int
and what if we don’t need decimals? Round’em!
public static function randInt(max:Number = 1,min:Number = 0):int{ returnMath.round(Math.random()*(max-min)+min); }
Random boolean
True or false? Flip a coin.
public static function bool():Boolean{ return(Math.round(Math.random()) == 0); }
Random sign
-1 or 1; Useful when you have to random select random rotation verse, for example, -1*45 or 1*45, rotates counterclockwise or clockwise.
public static function sign():Number{ return(Math.round(Math.random()) == 0):-1:1; }
Random color
Really useful when drawing debug shapes on screen..
public static functioncolor():uint{ returnMath.random()* 0xFFFFFF; }
Random point
Inside an area. I like to use the flash.geom.Rectangle struct to describe an area, more than using 4 parameters (Flash api are quite inconsistent about this choice)
public static function point(area:Rectangle):Point{ returnnewPoint(randInt(area.x, area.x+ area.width), randInt(area.y, area.y+ area.height)); }
Just a quick reminder on how to shut up the master volume on a flash project. I like a smooth tween to the silence, so I’m using the TweenLite libraries.
The trick here is to tween the volume property of a throw-away SoundTransform object, and on each step of the Tween apply that object as the SoundMixer.soundTransform Object, with an helper function. Note that I’m not going to write the function inside the TweenLite’s partameter list, as it is gross and dirty.
Tweening the volume back to 1 is so easy that I’m not writing it, to not offend anyone.
One class i really do like is TweenLite. I don’t really like the default tweening engine for a couple of reasons, but mainly because i have to save the tween object in a reference in my class, or the garbage collector could eat it (stopping the tweens in a unpredictable status).
TweenLite don’t require this (it exposes static functions, and keep internal references to the tweens) and permits me to specify functions to be called, not only handlers (which i know, may sound a little ActionScript 2.0, but in fact permits to call any funcion, and so avoiding one-line handlers).
One small example, (to be used inside a DisplayObjectContainer) is fading out a child, and detaching after. In one clear line: