Monthly Archives: November 2008

10,000 35 ways, .toString(radix);

Actionscript:
  1. for(var i:int = 2; i<37; i++){
  2.     trace((10000).toString(i));
  3. }
  4. /*
  5. outputs:
  6. 10011100010000
  7. 111201101
  8. 2130100
  9. 310000
  10. 114144
  11. 41104
  12. 23420
  13. 14641
  14. 10000
  15. 7571
  16. 5954
  17. 4723
  18. 3904
  19. 2e6a
  20. 2710
  21. 20a4
  22. 1cfa
  23. 18d6
  24. 1500
  25. 11e4
  26. kec
  27. iki
  28. h8g
  29. g00
  30. ekg
  31. dja
  32. cl4
  33. bpo
  34. b3a
  35. aci
  36. 9og
  37. 961
  38. 8m4
  39. 85p
  40. 7ps
  41. */

I had either totally forgotten about or never known about the Number, uint, int etc... .toString() radix argument. Brock mentioned it to me in conversation and I've been having fun with it ever since. It's especially nice for tracing out hex numbers:

Actionscript:
  1. var red:Number = 0xFF0000;
  2. trace("decimal:");
  3. trace(red);
  4. trace("hex:");
  5. trace(red.toString(16));

Posted in Number, strings | Tagged , , | Leave a comment

Mini Tween Engine

Actionscript:
  1. var box:MovieClip = new MovieClip();
  2. box.graphics.beginFill(0xFF0000);
  3. box.graphics.drawRect(-25,-25,50,50);
  4. addChild(box);
  5.  
  6. stage.addEventListener(MouseEvent.CLICK, onStageDown);
  7. function onStageDown(evt:MouseEvent):void{
  8.     moveTo(box, "x", 300, 2, Back.easeOut);
  9.     moveTo(box, "y", 200, 2, Back.easeOut);
  10.     moveTo(box, "scaleX", 2, 2, Back.easeOut);
  11.     moveTo(box, "rotation", 180, 2, Quartic.easeOut, onDone);
  12.     stage.removeEventListener(MouseEvent.CLICK, onStageDown);
  13. }
  14.                      
  15. function onDone():void {
  16.     moveTo(box, "x", 120, 2, Back.easeIn);
  17. }
  18.  
  19. //
  20. // -- TWEEN ENGINE
  21. //
  22. import fl.motion.easing.*;
  23.  
  24. // movieClip to tween, property to tween, final value for the property, duration of tween,
  25. // ease type, complete callback function
  26. function moveTo(mc:MovieClip, prop:String, dest:Number, duration:Number, ease=null,
  27.                            completeFunction:Function=null):void {
  28.     if (ease == null) {
  29.         ease = Quartic.easeOut;
  30.     }
  31.     // use the property to make all var names unique
  32.     mc["begin" + prop] = mc[prop];
  33.     mc["change" + prop] =  dest - mc[prop];
  34.     mc["time" + prop] = 0;
  35.     mc["startTime" + prop] = getTimer();
  36.     mc["duration" + prop] = duration;
  37.     mc["prop"] = prop;
  38.     mc["ease" + prop] = ease;
  39.     mc["complete_" + prop] = completeFunction;
  40.     mc.addEventListener(Event.ENTER_FRAME, makeMotionFunction(mc, prop));
  41. }
  42.  
  43. function makeMotionFunction(mc:MovieClip, prop:String):Function {
  44.     return function(evt:Event){
  45.         if (mc["time" +prop] <= mc["duration"+ prop]){
  46.            mc[prop]=mc["ease" + prop](mc["time" + prop], mc["begin" +prop], mc["change"+ prop], mc["duration"+ prop]);
  47.         } else {
  48.             var completeFunction:Function = mc["complete_"+prop];
  49.             if (completeFunction!=null){
  50.               completeFunction();
  51.             }
  52.             mc.removeEventListener(Event.ENTER_FRAME, arguments.callee);
  53.             // see how long the tween took
  54.             //trace(mc["time"+prop]);
  55.         }
  56.            mc["time" + prop] = (getTimer() - mc["startTime" + prop]) / 1000;
  57.     };
  58. }
  59. /*
  60. WARNING: This code was written for fun. Use at your own risk.
  61. */

This code uses a ~35 line tweening engine. I wrote this for fun a few weeks back. It works by creating dynamic variables on the MovieClip that it tweens. The dynamic variables and the square bracket syntax cause it to be pretty slow. Definitely not a substitute for a real tweening engine... like TweenLite.

I wouldn't recommend using this for anything other than playing around. See warning page for more info.

Posted in dynamic, motion, properties | Leave a comment

Number.toFixed()

Actionscript:
  1. var someValue:Number = 2.0480531167374427;
  2.  
  3. // want to show only 3 decimal places?
  4. trace(someValue.toFixed(3)); // outputs 2.048

An easy way to get rid of extra decimal values. This is useful when you want to show decimal values in text fields. I only recently noticed this in the docs, I used to do this type of thing:

Actionscript:
  1. trace(int(someValue * 1000) / 1000);  // outputs 2.048

Posted in Number | Tagged , | 4 Comments

XOR Color Invert

Actionscript:
  1. var yellow:uint = 0xFFFF00;
  2.  
  3. // draws yellow circle
  4. with(graphics) beginFill(yellow), drawCircle(100,100,50);
  5.  
  6. // invert the color using XOR assignment
  7. // yellow becomes 0x0000FF
  8. yellow ^= 0xFFFFFF;
  9.  
  10. // draws blue  circle
  11. with(graphics) beginFill(yellow), drawCircle(200,100,50);

Just a fun use for XOR. You could also do it without XOR assignment:

Actionscript:
  1. with(graphics) beginFill(yellow ^ 0xFFFFFF), drawCircle(200,100,50);

Playing a little with bitwise operators is a good way to make sure you understand them. Try tweaking this trace statement:

Actionscript:
  1. trace((0x543210 ^ 0xFFFFFF).toString(16));

If you'd like to read about bitwise operators, I recommend wikipedia's article.

Posted in Operators, one-liners | Tagged , , | Leave a comment