Actionscript 3: Get ratio value between two numbers

Simple utility for getting a ratio between two numbers. Thanks to my friend Jamie for the method.


private function returnValue(value:Number, actualMin:Number, actualMax:Number, ratioMin:Number, ratioMax:Number):Number{
     return (((value - actualMin) / (actualMax - actualMin)) * (ratioMax - ratioMin)) + ratioMin;
}

Here is also a simpler version when you know the percentage of the number range.


public function returnValue( percentage:Number, min:Number, max:Number ):Number {
    return ((( max - min ) / 100 ) * percentage ) + min;
}

This version returns a percentage of the two range of numbers.


public  function returnValuePercentage( _value:Number, min:Number, max:Number ):Number {
return ((( _value - min ) / ( max - min )) * 100 );
 }

[ad#Google Adsense]

Actionscript 3: Detect the difference between a Mouse click and a Mouse drag.

How do you tell if the user clicked or dragged in your flash app? Take a look:

private var time:uint;

function onMouseDown(event:Event):void {
     time = getTimer();
     startDrag();
}

function onMouseUp(event:Event):void {
     var diff = getTimer() - time;
     if (diff < 250) {
           trace("clicked");
     }
     stopDrag();
}

[ad#Google Adsense]

Actionscript 3: Move first array index to the back and back to front

Here is a very cool line of code to manipulate an array.

If you want to take the first item of an array and place it at the end:

myArray.push(myArray.shift());

If you want to take the last item of an array and place it back at the front:

myArray.unshift(myArray.pop());

[ad#Google Adsense]

Actionscript 3: Check if flash is stand alone or in browser

Here is a quick code snippet if you want to have links trace the value locally but link out in the browser.


if (Capabilities.playerType == "StandAlone" || Capabilities.playerType == "External") {
	trace("data.CTAOpen.@url : " + data.CTAOpen.@url );
} else {
	navigateToURL(new URLRequest(data.CTAOpen.@url), "_self");
}

Or you can use RegExp

var isStandAlone:Boolean = new RegExp("file://").test(loaderInfo.url);

[ad#Google Adsense]

Actionscript 3: Remove all elements from a Movieclip

I’m always trying to empty out a clip and this is by far the best way :

while (target_mc.numChildren) {
target_mc.removeChildAt(0);
}

or if you prefer to be verbose :

var total:uint = target_mc.numChildren;
for (var i:uint = 0; i < total; i++) {
target_mc.removeChildAt(0);
}

or if you prefer single function :

for (var i:uint = 0; i < target_mc.numChildren; i++) {
target_mc.removeChildAt(0);
i--;
}

Here is a user submitted version :

while (target_mc.numChildren > 0) {
target_mc.removeChildAt(target_mc.numChildren - 1);
}

Here is a one line version :

while (target_mc.numChildren != 0) target_mc.removeChildAt(0);

Saw this one on Adobe website :

while (target_mc.numChildren > 0) target_mc.removeChildAt(0);

I think that’s it.

[ad#Google Adsense]