DisplayObject describeType()

Actionscript:
  1. for each(var prop:String in describeType(DisplayObject).factory.accessor.@name){
  2.     trace(prop);
  3. }
  4. /* outputs
  5. scaleY
  6. mouseX
  7. mouseY
  8. mask
  9. rotation
  10. alpha
  11. transform
  12. blendMode
  13. x
  14. root
  15. loaderInfo
  16. width
  17. z
  18. rotationX
  19. scale9Grid
  20. filters
  21. rotationY
  22. y
  23. stage
  24. scaleZ
  25. parent
  26. accessibilityProperties
  27. scrollRect
  28. rotationZ
  29. height
  30. name
  31. opaqueBackground
  32. blendShader
  33. cacheAsBitmap
  34. visible
  35. scaleX
  36. */

I First saw describeType() at senocular's AS3 Tip of the Day on Kirupa.

Posted in DisplayObject, XML | 1 Comment

Random Choice

Actionscript:
  1. function randomChoice(...args:Array):*{
  2.     return args[int(Math.random()*args.length)];
  3. }
  4.  
  5. // choose randomly from a number of different values
  6. var val:* = randomChoice(10, "Hello", "0xFF0000", 1000);
  7.  
  8. // choose a random color (in this case red, green or blue)
  9. var randColor:uint = randomChoice(0xFF0000, 0x00FF00, 0x0000FF);
  10.  
  11. trace(val);
  12. trace(randColor);

The function randomChoice() takes any number of arguments and spits one of them back randomly. Just a handy function to have around... I especially like to use this function to choose from a handful of colors.

Posted in functions, random | 2 Comments

Rect vs Ellipse

Actionscript:
  1. for (var i:int = 0; i<100; i++){
  2.     graphics.lineStyle(0,0);
  3.     graphics[["drawEllipse", "drawRect"][int(Math.random()*2)]](Math.random()*400, Math.random()*300, Math.random()*100,Math.random()*100)
  4. }
  5.  
  6. /*
  7. WARNING: This code was written for fun. Use at your own risk.
  8. */

And a more readable version:

Actionscript:
  1. var methodChoices:Array = ["drawEllipse", "drawRect"];
  2. var method:String;
  3. var xp:Number, yp:Number, w:Number, h:Number;
  4. for (var i:int = 0; i<100; i++){
  5.     method = methodChoices[int(Math.random()*methodChoices.length)];
  6.     graphics.lineStyle(0,0);
  7.     xp = Math.random()*400;
  8.     yp = Math.random()*300;
  9.     w = Math.random()*100;
  10.     h = Math.random()*100;
  11.     graphics[method](xp, yp, w, h)
  12. }
  13.  
  14. /*
  15. WARNING: This code was written for fun. Use at your own risk.
  16. */

Here I use yesterdays associative array function call technique to do something different. Not really all that useful, but interesting....

Posted in Graphics, associative arrays, functions, random | Leave a comment

Associative Array Function Call

Actionscript:
  1. for (var i:int = 0; i<4; i++){
  2.     this["phase"+i]();
  3. }
  4.  
  5. function phase0():void{
  6.     trace("phase 0");
  7. }
  8. function phase1():void{
  9.     trace("phase 1");
  10. }
  11. function phase2():void{
  12.     trace("phase 2");
  13. }
  14. function phase3():void{
  15.     trace("phase 3");
  16. }
  17.  
  18. /*
  19. will output:
  20. phase 0
  21. phase 1
  22. phase 2
  23. phase 3
  24. */
  25.  
  26. /*
  27. WARNING: This code was written for fun. Use at your own risk.
  28. */

I was pleasantly surprised back in the AS1 days... when I discovered that associative array syntax worked along with function calls (why wouldn't it?). There are some interesting tricks you can do with this technique.... will post some later....

Posted in arrays, associative arrays, functions | Leave a comment