PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/Flash.as

http://github.com/Rahil627/ActionScript-Library
ActionScript | 299 lines | 168 code | 42 blank | 89 comment | 27 complexity | de5a710c0916bb479073d34596792a7b MD5 | raw file
  1. package rahil {
  2. import flash.display.BitmapData;
  3. import flash.display.DisplayObject;
  4. import flash.display.DisplayObjectContainer;
  5. import flash.display.Sprite;
  6. import flash.geom.Matrix;
  7. import flash.geom.Point;
  8. import flash.geom.Rectangle;
  9. import flash.text.Font;
  10. import flash.utils.ByteArray;
  11. /**
  12. * My personal library for Flash
  13. * @author Rahil Patel
  14. */
  15. public class Flash {
  16. public static const RADIAN:Number = Math.PI / 180; //FP.RAD uses /-180
  17. public static const DEGREE:Number = 180 / Math.PI;
  18. /*
  19. * Removes all of the children of the object
  20. */
  21. public static function removeAllChildren(object:DisplayObjectContainer):void { //not really needed, unless you want to remove everything from the topmost object
  22. while (object.numChildren > 0)
  23. object.removeChildAt(0);
  24. }
  25. /*
  26. * Removes all of the children of the object and itself
  27. */
  28. public static function removeAllChildrenAndSelf(object:DisplayObjectContainer):void {
  29. removeAllChildren(object);
  30. object.parent.removeChild(object);
  31. }
  32. /**
  33. * Converts degrees into radians
  34. */
  35. public static function convertToRadians(degrees:Number):Number {
  36. return degrees * Math.PI / 180;
  37. }
  38. public static function degreesToRadians(degrees:Number):Number {
  39. return degrees * Math.PI / 180;
  40. }
  41. /**
  42. * Converts radians into degrees
  43. */
  44. public static function convertToDegrees(radians:Number):Number {
  45. return radians * 180 / Math.PI;
  46. }
  47. public static function radiansToDegrees(radians:Number):Number {
  48. return radians * 180 / Math.PI;
  49. }
  50. /**
  51. * Returns a random whole number within the range provided
  52. */
  53. public static function randomNumber(low:Number = 0, high:Number = 1):Number {
  54. return Math.floor(Math.random() * (1 + high - low)) + low;
  55. }
  56. /**
  57. * Returns true if successful
  58. * @param chance - chance of success, range of 0-1
  59. */
  60. public static function chance(chance:Number):Boolean {
  61. return (Math.random() < chance);
  62. }
  63. /**
  64. * Returns the number of pixels within the bitmap that do not have a uint value of 0
  65. */
  66. public static function numberOfPixelsInBitmapData(bitmapData:BitmapData):uint {
  67. var pixelVector:Vector.<uint> = bitmapData.getVector(bitmapData.rect);
  68. return numberOfPixelsInVector(pixelVector);
  69. }
  70. /**
  71. * Returns the number of pixels within the pixel vector of a bitmap that do not have a uint value of 0
  72. */
  73. public static function numberOfPixelsInVector(pixelVector:Vector.<uint>):uint {
  74. var numberOfPixels:uint;
  75. for each (var pixel:uint in pixelVector) {
  76. if (pixel != 0)
  77. numberOfPixels++;
  78. }
  79. return numberOfPixels;
  80. }
  81. /**
  82. * Returns a 2d map of pixel colors (uint)
  83. */
  84. public static function getPixelArray2d(bitmapData:BitmapData):Array {
  85. var rows:int = bitmapData.height;
  86. var cols:int = bitmapData.width;
  87. var pixelArray2d:Array = new Array(rows);
  88. var row:Array;
  89. for (var i:int = 0; i < rows; i++) {
  90. row = [];
  91. for (var j:int = 0; j < cols; j++) {
  92. row[j] = bitmapData.getPixel32(j, i); // >> 24 & 0xFF;
  93. }
  94. pixelArray2d[i] = row;
  95. }
  96. return pixelArray2d;
  97. }
  98. /**
  99. * Traces out a 2d pixel map
  100. */
  101. public static function debugPixelArray2d(pixelArray2d:Array):void {
  102. var cols:int = pixelArray2d.length
  103. var rows:int = (pixelArray2d[0] as Array).length;
  104. var s:String;
  105. for (var i:int = 0; i < rows; i++) {
  106. s = "";
  107. for (var j:int = 0; j < cols; j++) {
  108. s = s + (pixelArray2d[i][j] == 0 ? 0 : 1); //0s and 1s
  109. }
  110. trace(s);
  111. }
  112. }
  113. /**
  114. * Traces out a 2D pixel map
  115. */
  116. public static function debugBitmapData(bitmapData:BitmapData):void {
  117. var pixelTable:Array = getPixelArray2d(bitmapData);
  118. debugPixelArray2d(pixelTable);
  119. }
  120. /**
  121. * Returns -1 or 1
  122. */
  123. public static function sign(n:Number):int {
  124. return n < 0 ? -1 : 1;
  125. }
  126. //by senocular, should check out his utilities!
  127. /**
  128. * Returns a deep copy of an object
  129. * Warning: it returns an Object, so remember to cast it back yourself
  130. * Also, it seems to only work for Arrays and Dictionaries, not any object
  131. */
  132. public static function deepCopy(source:Object):* {
  133. var copier:ByteArray = new ByteArray();
  134. copier.writeObject(source);
  135. copier.position = 0;
  136. return (copier.readObject());
  137. }
  138. /**
  139. * Returns a shallow copy of an object
  140. * Warning: it returns an Object, so remember to cast it back yourself
  141. */
  142. public static function shallowCopy(sourceObj:Object):Object {
  143. //var cls:Class = getDefinitionByName(getQualifiedClassName(sourceObj)) as Class;
  144. //var copyObject:Object = new cls(); //create an instance of the same class as objectA
  145. var copyObject:Object = new Object();
  146. for (var property:String in sourceObj) //loop through the properties of the object
  147. copyObject[property] = sourceObj[property];
  148. return copyObject;
  149. }
  150. public static function uintToHex(n:uint):String {
  151. return n.toString(16);
  152. //apparently the above implementation fails with zero spacing on some occasions
  153. /*
  154. var digits:String = "0123456789ABCDEF";
  155. var hex:String = '';
  156. while (dec > 0){
  157. var next:uint = dec & 0xF;
  158. dec >>= 4;
  159. hex = digits.charAt(next) + hex;
  160. }
  161. if (hex.length == 0)
  162. hex = '0'
  163. return hex;
  164. */
  165. }
  166. public static function setRegistrationPoint(s:Sprite, regx:Number, regy:Number, showRegistration:Boolean = false):void {
  167. //translate movieclip
  168. s.transform.matrix = new Matrix(1, 0, 0, 1, -regx, -regy);
  169. //registration point.
  170. if (showRegistration) {
  171. var mark:Sprite = new Sprite();
  172. mark.graphics.lineStyle(1, 0x000000);
  173. mark.graphics.moveTo(-5, -5);
  174. mark.graphics.lineTo(5, 5);
  175. mark.graphics.moveTo(-5, 5);
  176. mark.graphics.lineTo(5, -5);
  177. //s.parent.addChild(mark);
  178. s.addChild(mark);
  179. }
  180. }
  181. /**
  182. * display.tranform matrix by a registration point
  183. */
  184. public static function setDisplayMatrix(display:DisplayObject, tx:Number = 0, ty:Number = 0, reg:Point = null, ang:Number = 0, scalex:Number = 1, scaley:Number = 1, skew:Number = 0, axis:String = "x"):void {
  185. reg = reg || new Point();
  186. var curM:Matrix = new Matrix();
  187. var skewM:Matrix = new Matrix();
  188. var rotM:Matrix = new Matrix();
  189. var scaleM:Matrix = new Matrix();
  190. var rp:Point;
  191. var r:Number = ang * Math.PI / 180;
  192. if (axis == "y") {
  193. skewM.c = Math.tan(skew);
  194. } else {
  195. skewM.b = Math.tan(skew);
  196. }
  197. scaleM.scale(scalex, scaley);
  198. rotM.rotate(r);
  199. curM.concat(scaleM);
  200. curM.concat(skewM);
  201. curM.concat(rotM);
  202. rp = curM.transformPoint(reg);
  203. curM.tx = -rp.x;
  204. curM.ty = -rp.y;
  205. curM.tx += tx;
  206. curM.ty += ty;
  207. display.transform.matrix = curM;
  208. }
  209. /**
  210. * Traces out if any embedded fonts were found or not
  211. */
  212. public static function debugEmbeddedFonts():void {
  213. var a:Array = Font.enumerateFonts(false);
  214. if (a.length > 0)
  215. trace("embedded font found: " + a[0]["fontName"]);
  216. else
  217. trace("no embedded fonts");
  218. }
  219. public static function getRandomPointWithinRectangle(rectangle:Rectangle):Point {
  220. var randomX:int = randomNumber(rectangle.left, rectangle.right);
  221. var randomY:int = randomNumber(rectangle.top, rectangle.bottom);
  222. return new Point(randomX, randomY);
  223. }
  224. /**
  225. * Searches spirally outward from the inputted location
  226. * The function is not meant to be used, it's just here for keepsake
  227. * @param X max width of the search
  228. * @param Y max height of the search
  229. */
  230. public static function spiralSearch(X:int, Y:int):void {
  231. var cx:int = 0;
  232. var cy:int = 0;
  233. var dx:int = 0;
  234. var dy:int = -1;
  235. for (var i:int = 0; i < Math.pow(Math.max(X, Y), 2); i++) {
  236. if ((-X / 2 < cx <= X / 2) && (-Y / 2 < cy <= Y / 2))
  237. trace(cx, cy); //do stuff
  238. if (cx == cy || (cx < 0 && cx == -cy) || (cx > 0 && cx == 1 - cy)) {
  239. var dxTemp:int = dx;
  240. var dyTemp:int = dy;
  241. dx = -dyTemp;
  242. dy = dxTemp;
  243. }
  244. cx = cx + dx;
  245. cy = cy + dy;
  246. }
  247. }
  248. /*
  249. public static function openURL(url:String):void {
  250. navigateToURL(new URLRequest(url));
  251. }
  252. */
  253. }
  254. }