Hello EveryOne !! 🙂

When we are working with Flash Games or Applications at the time some small functional codes are repeating like random number generation , string handling and Array handling. So creating library of reusable code snippets is a great way to speed up development.

I given below some small repository of actionscript code.

**Generate Random Numbers**

function randomRange(minNum:Number, maxNum:Number):Number { return (Math.floor(Math.random() * (maxNum - minNum + 1)) + minNum); }

**Find the Angle Between Two Points**

function getAngle (x1:Number, y1:Number, x2:Number, y2:Number):Number { var dx:Number = x2 - x1; var dy:Number = y2 - y1; return Math.atan2(dy,dx); }

**Convert Degrees To Radians And Radians To Degrees**

var degrees = 90; // Test Value var radians = degreesToRadians(degrees); degrees = radiansToDegrees(radians); function degreesToRadians(degrees:Number):Number { return degrees * Math.PI / 180; } function radiansToDegrees(radians:Number):Number { return radians * 180 / Math.PI; }

**Generate Random String**

function generateRandomString(strlen:Number):String { var chars:String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; var num_chars:Number = chars.length - 1; var randomChar:String = ""; for (var i:Number = 0; i < strlen; i++) { randomChar += chars.charAt(Math.floor(Math.random() * num_chars)); } return randomChar; } trace(" Generated String :: "generateRandomString(5));

**Remove The Whitespace From String**

var str:String = "action scri pt"; function removeWhiteSpace(str:String):String { var newstring = str.split(" ").join(""); return newstring; } trace(removeWhiteSpace(str));

**Count The No Of Occurrences of a Character In a String**

var str:String = "Moorthy"; var char:String = "o"; function countOccurences(str:String, char:String):Number { var count:Number = 0; for(var i=0; i < str.length; i++) { if (str.charAt(i) == char) { count++; } } return count; } trace(countOccurences(str, char));

**Search And Replace Characters In String**

var str:String = "AS3 in Flash!!"; var search:String = "AS3"; var replace:String = "Actionscript 3"; function strReplace(str:String, search:String, replace:String):String { return str.split(search).join(replace); } trace(strReplace(str, search, replace));

**Reverse A String**

var myString:String = "Moorthy"; var resultStr:String= myString.split("").reverse().join(""); trace(resultStr);

**Find Min Value /Max Value Of Array Of Integers**

var min:Number; var max:Number; var numbers:Array = new Array(3, 5, 2, 1, 4); function findMinValue(array:Array):Number { min = array[0]; for (var i = 0; i < array.length; i++) { if (array[i] < min) { min = array[i]; } } return min; } function findMaxValue(array:Array):Number { max = array[0]; for (var i = 0; i < array.length; i++) { if (array[i] > max) { max = array[i]; } } return max; } trace(findMinValue(numbers)); trace(findMaxValue(numbers));

**Time Format**

function formatTime (time:Number):String { var remainder:Number; var hours:Number = time / ( 60 * 60 ); remainder = hours - (Math.floor ( hours )); hours = Math.floor ( hours ); var minutes = remainder * 60; remainder = minutes - (Math.floor ( minutes )); minutes = Math.floor ( minutes ); var seconds = remainder * 60; remainder = seconds - (Math.floor ( seconds )); seconds = Math.floor ( seconds ); var hString:String = hours < 10 ? "0" + hours : "" + hours; var mString:String = minutes < 10 ? "0" + minutes : "" + minutes; var sString:String = seconds < 10 ? "0" + seconds : "" + seconds; if ( time < 0 || isNaN(time)) return "00:00"; if ( hours > 0 ) { return hString + ":" + mString + ":" + sString; }else { return mString + ":" + sString; } } trace("...."+ formatTime(100));

My hope this is useful for beginners, and you can see more useful snippet from here Snipplr

Cheers,

**Kutty**