Tag Archives: flash

Array map

Actionscript:
  1. var a:Array = [1,2,3,4,5,6];
  2. var double:Array = a.map(function():int{return arguments[0] * 2});
  3. trace(double);
  4. /*outputs:
  5. 2,4,6,8,10,12
  6. */

A condensed example of Array.map()

Here is the same thing written in haskell:

a = [1,2,3,4,5,6]
double = map (*2) a

main = print double
Posted in arrays, functions | Also tagged , | Leave a comment

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.

Posted in functions, misc | Also tagged , | 7 Comments

zip Function for Arrays

Actionscript:
  1. var one:Array = [1,2,3];
  2. var two:Array = [10, 20, 30];
  3.  
  4. var zipOneTwo:Array = zip(one, two);
  5.  
  6. // trace each tupple
  7. for each (var tuple:Array in zipOneTwo){
  8.     trace(tuple);
  9. }
  10.  
  11. /* outputs:
  12. 1,10
  13. 2,20
  14. 3,30
  15. */
  16.  
  17. function zip(a:Array, b:Array):Array{
  18.     var longest:Array = (a.length>= b.length) ? a : b;
  19.     var zipped:Array = [];
  20.     for (var i:int = 0; i<longest.length; i++){
  21.         zipped.push([a[i], b[i]]);
  22.     }
  23.     return zipped;
  24. }

This snippet shows a function called zip that takes two arrays and returns a two dimensional array of tuples. Just imagine that each array is one side of a zipper and you'll sort of get the idea...

I do wish flash would trace this:

[[1, 10], [2, 20], [3, 30]]

We shouldn't have to write a utility function to see the real array structure...

I've been messing with haskell for a few days now... just for fun I thought I'd write a few functions inspired by it... this is the first one...

Posted in arrays | Also tagged , | 3 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

Posted in DisplayObject, misc, motion | Also tagged , | 1 Comment