Category Archives: misc

Recursive Countdown

Actionscript:
  1. loop(20);
  2.  
  3. function loop(i:int):void {
  4.     if (i <0) return;
  5.       trace(i);
  6.       loop(i - 1);
  7. }
  8.  
  9. /* outputs:
  10. 20
  11. 19
  12. 18
  13. 17
  14. 16
  15. 15
  16. 14
  17. 13
  18. 12
  19. 11
  20. 10
  21. 9
  22. 8
  23. 7
  24. 6
  25. 5
  26. 4
  27. 3
  28. 2
  29. 1
  30. 0
  31. */

This snippet uses a recursive function to count down from some number. Recursion is pretty useless in actionscript, it will eventually cause an error... If you were to try to countdown from a higher number it would choke pretty fast...

Been writing haskell lately so I have recursion on the brain.

Also posted in functions | Tagged , , | 7 Comments

Rotation Property Weirdness

Actionscript:
  1. var boxA:Shape = Shape(addChild(new Shape()));
  2. with (boxA.graphics) beginFill(0), drawRect(-10,-10,20,20);
  3.  
  4. var boxB:Shape = Shape(addChild(new Shape()));
  5. with (boxB.graphics) beginFill(0), drawRect(-10,-10,20,20);
  6.  
  7. boxA.x = 100;
  8. boxA.y = 100;
  9.  
  10. boxB.x = 200;
  11. boxB.y = 100;
  12.  
  13. var rot:Number = 32750;
  14.  
  15. addEventListener(Event.ENTER_FRAME, onLoop);
  16. function onLoop(evt:Event):void {
  17.   rot += 1
  18.   // will stop rotating
  19.   boxA.rotation = rot
  20.   // will keep rotating
  21.   boxB.rotation = rot % 360;
  22. }

I recently became aware of a strange aspect of the rotation property on DisplayObjects. For some reason, once it's value goes a little beyond ~32750 the DisplayObject will simply stop rotating. If you read the rotation property it is still changing, but there is no visual update - a quick check on the DisplayObject.transform.matrix property will show that the value has stopped.

The easy fix is to use mod before applying the value to the rotation property. Surprised I've never come across this one before. Maybe someone can shed some light on this.


// for people searching google for solutions to this problem I'll add the following key words:
MovieClip stops rotating, DisplayObject stops rotating, rotation property broken, not rotating

Also posted in DisplayObject, motion | Tagged , , | 1 Comment

Paste an Arbitrary Piece of Code

Actionscript:
  1. ...
  2. // loop through until we find the root note
  3.     // grab the third and the fifth and exit the loop
  4.     for (var i:int = 0; i<leng; i++){
  5.         if (cMajor[i] == note){
  6.             third = cMajor[(i + 2) % leng];
  7.             fifth = cMajor[(i + 4) % leng];
  8.             break;
  9.         }
  10.     }
  11.    
  12.     // we may need a double sharp on the middle note
  13.     var sharpFlatDouble:String = sharpFlat;
  14.    
  15.     // check if this is a sharp, check if it is A or D
  16.     // if it is add the symbol for double sharp
  17.     if (sharpFlat == "#"){
  18.         if (note == "D" || note == "A"){
  19.             sharpFlatDouble = "x";
  20.         }
  21.     }
  22. ...

If your working on some code... just randomly copy a piece of it and paste it in the comments... This code is from a program that generates any major scale (it's still not finished). Feel free to post code chunks in any language...

[EDIT] the code doesn't need to work on its own... you can just randomly copy from something your working on...

Posted in misc | Tagged , , | 15 Comments

Float as a Fraction

Actionscript:
  1. // print some floats as fractions
  2. printFraction(0.5);
  3. printFraction(0.75);
  4. printFraction(0.48);
  5.  
  6. // try something a little more complex
  7. var float:Number = 0.98765432;
  8. trace("\na more complex example:");
  9. printFraction(float);
  10. var frac:Array = asFraction(float);
  11. trace("double check it:");
  12. trace(frac[0] + "/" + frac[1] +" = " + frac[0] / frac[1]);
  13.  
  14. /* outputs:
  15. 0.5 = 1/2
  16. 0.75 = 3/4
  17. 0.48 = 12/25
  18.  
  19. a more complex example:
  20. 0.98765432 = 12345679/12500000
  21. double check it:
  22. 12345679/12500000 = 0.98765432
  23. */
  24.  
  25.  
  26. function printFraction(n:Number):void{
  27.     var frac:Array = asFraction(n);
  28.     trace(n + " = " + frac[0] + "/" + frac[1]);
  29. }
  30.  
  31. // takes any value less than one and returns an array
  32. // with the numerator at index 0 and the denominator at index 1
  33. function asFraction(num:Number):Array{
  34.     var decimalPlaces:int = num.toString().split(".")[1].length;
  35.     var denom:Number = Math.pow(10, decimalPlaces);
  36.     return reduceFraction(num * denom, denom);
  37. }
  38.  
  39. // divide the numerator and denominator by the GCF
  40. function reduceFraction(numerator:int, denominator:Number):Array{
  41.     // divide by the greatest common factor
  42.     var divisor:int = gcf(numerator, denominator);
  43.     if (divisor){
  44.         numerator /= divisor;
  45.         denominator /= divisor;
  46.     }
  47.     return [numerator, denominator];
  48. }
  49.                    
  50. // get the greatest common factor of two integers
  51. function gcf(a:int, b:int):int{
  52.     var remainder:int;
  53.     var factor:Number = 0;
  54.     var maxIter:int = 100;
  55.     var i:int = 0;
  56.     while (1){
  57.         if (b> a){
  58.            var swap:int = a;
  59.            a = b;
  60.            b = swap;
  61.         }
  62.         remainder = a % b;
  63.         a = b;
  64.         b = remainder
  65.         if (remainder == 0){
  66.             factor = a;
  67.             break;
  68.         }else if (remainder==1){
  69.             break;
  70.         }else if (i> maxIter){
  71.             trace("failed to calculate gcf");
  72.             break;
  73.         }
  74.         i++;
  75.     }
  76.     return factor;
  77. }

This snippet contains a few functions for calculating fractions based on float values. Writing this brought back some memories from grade school math.

Also posted in Math | Tagged , , | 4 Comments