Category Archives: misc

Flashvars

Actionscript:
  1. // variable is passed into the swf via Javascript (most likely swfObject)
  2. // myswf.swf?myFlashVar="a new value"
  3.  
  4. var myFlashVar:String = "Some Default Value";
  5. if (root.loaderInfo.parameters.myFlashVar){
  6.    myFlashVar = root.loaderInfo.parameters.myFlashVar;
  7. }

Getting at flashvars in AS3 requires a bit more typing than it did in AS2. Above is a technique I find myself using from time to time. Usually for paths to xml or dynamic callouts (large buttons with text, an image and a link that change frequently).

Also posted in dynamic | Tagged , | Leave a comment

Unique Array Comparison

Actionscript:
  1. function uniqueCompare(arr:Array, compare:Function):void {
  2.     var leng:int = arr.length;
  3.     for (var i:int = 0; i<leng; i++){
  4.         for (var j:int = i + 1; j<leng; j++){
  5.             compare(arr[i], arr[j]);
  6.         }
  7.     }
  8. }
  9.  
  10. var numbers:Array = [1, 2, 3, 4, 5];
  11.  
  12. uniqueCompare(numbers, compareCallback);
  13.  
  14. function compareCallback(a:*, b:*):void {
  15.     trace("compare " + a + " to " + b);
  16. }
  17.  
  18. /*
  19. outputs:
  20. compare 1 to 2
  21. compare 1 to 3
  22. compare 1 to 4
  23. compare 1 to 5
  24. compare 2 to 3
  25. compare 2 to 4
  26. compare 2 to 5
  27. compare 3 to 4
  28. compare 3 to 5
  29. compare 4 to 5
  30. */

This snippet shows a function called uniqueCompare() that compares all elements in an array to all other elements in an array without repeating a comparison.

Also posted in arrays | Tagged , | Leave a comment

Object.constructor()

Actionscript:
  1. var Box:Function = {
  2.     constructor : function(color:uint, w:Number, h:Number):void {
  3.         this.color = color;
  4.         this.s = new Shape();
  5.         with(this.s.graphics) beginFill(this.color), drawRect(0,0,w,h);
  6.         addChild(this.s);
  7.        
  8.         this.setLoc = function(x:Number, y:Number):void{
  9.             this.s.x = x;
  10.             this.s.y = y;
  11.        }
  12.     }
  13. }.constructor;
  14.  
  15. var box0:Object = new Box(0xFF0000, 100, 100);
  16.  
  17. box0.setLoc(100, 10);
  18.  
  19. var box1:Object = new Box(0x000000, 50, 50);
  20.  
  21. box1.setLoc(210, 10);

This snippet makes use of Object.constructor() to allow creation of Object instances using the new keyword. This is for fun only, I don't recommend this over actual Classes.

Also posted in OOP, dynamic, functions | Tagged , | Leave a comment

Line Intersection Part 2 (optimized)

Actionscript:
  1. // optimized line to line intersection test:
  2. // from wikipedia: http://en.wikipedia.org/wiki/Line-line_intersection
  3. // and http://local.wasp.uwa.edu.au/~pbourke/geometry/lineline2d/
  4. var ip:Point = new Point();
  5.  
  6. function intersection(p1:Sprite, p2:Sprite, p3:Sprite, p4:Sprite):Point {
  7.     var nx:Number, ny:Number, dn:Number;
  8.     var x4_x3:Number = p4.x - p3.x;
  9.     var y4_y3:Number = p4.y - p3.y;
  10.     var x2_x1:Number = p2.x - p1.x;
  11.     var y2_y1:Number = p2.y - p1.y;
  12.     var x1_x3:Number = p1.x - p3.x;
  13.     var y1_y3:Number = p1.y - p3.y;
  14.    
  15.     nx = x4_x3 * y1_y3 - y4_y3 * x1_x3;
  16.     ny = x2_x1 * y1_y3 - y2_y1 * x1_x3;
  17.     dn = y4_y3 * x2_x1 - x4_x3 * y2_y1;
  18.    
  19.     nx /= dn;
  20.     ny /= dn;
  21.    
  22.     // has intersection
  23.     if(nx>= 0 && nx <= 1 && ny>= 0 && ny <= 1){
  24.         ny = p1.y + nx * y2_y1;
  25.         nx = p1.x + nx * x2_x1;
  26.         ip.x = nx;
  27.         ip.y = ny;
  28.     }else{
  29.         // no intersection
  30.         ip.x = ip.y = -1000;
  31.     }
  32.     return ip
  33. }
  34.  
  35.  
  36. //
  37. // test out the function:
  38. //
  39.  
  40. stage.frameRate = 30;
  41. var a:Sprite = dot();
  42. a.x = a.y = 100;
  43.  
  44. var b:Sprite = dot();
  45. b.x = b.y = 200;
  46.  
  47. var c:Sprite = dot();
  48. c.x = 200
  49. c.y = 100;
  50.  
  51. var d:Sprite = dot();
  52. d.x = 100
  53. d.y = 200;
  54.  
  55. var inter:Sprite = dot(0xFF0000,true);
  56.  
  57. addEventListener(Event.ENTER_FRAME, onLoop);
  58. function onLoop(evt:Event):void {
  59.     var p:Point = intersection(a, b, c, d);
  60.     inter.x = p.x;
  61.     inter.y = p.y
  62.    
  63.     with(graphics){
  64.         clear();
  65.         lineStyle(0,0x000000);
  66.         moveTo(a.x, a.y);
  67.         lineTo(b.x, b.y);
  68.         moveTo(c.x, c.y);
  69.         lineTo(d.x,d.y);
  70.     }
  71. }
  72.  
  73. // draggable dot
  74. function dot(col:uint = 0x507399, noDrag:Boolean = false):Sprite {
  75.     var s:Sprite = Sprite(addChild(new Sprite));
  76.     with(s.graphics) beginFill(col), drawCircle(0,0,5);
  77.     if (!noDrag){
  78.         s.buttonMode = true;
  79.        s.addEventListener(MouseEvent.MOUSE_DOWN, onDrag);
  80.     }
  81.     return s;
  82. }
  83. function onDrag(evt:MouseEvent):void {
  84.     evt.currentTarget.startDrag()
  85. }
  86.  
  87. stage.addEventListener(MouseEvent.MOUSE_UP, onUp);
  88. function onUp(evt:MouseEvent):void{
  89.     stopDrag();
  90. }

This is a significantly optimized version of yesterdays post. The function intersection() takes four sprites as its arguments. The four arguments could be considered starting points and ending points for two line segments.

The bottleneck in yesterdays post looked something like this;

nx = (p1.x * p2.y - p1.y * p2.x) * (p3.x - p4.x) - (p1.x - p2.x) * (p3.x * p4.y - p3.y * p4.x);
dn = (p1.x - p2.x) * (p3.y - p4.y) - (p1.y - p2.y) * (p3.x - p4.x);
ny = (p1.x * p2.y - p1.y * p2.x) * (p3.y - p4.y) - (p1.y - p2.y) * (p3.x * p4.y - p3.y * p4.x);
return new Point(nx /dn, ny / dn);

nx and ny are the numerators for two fractions both with dn as their denominator.... after taking a look at this page:

http://local.wasp.uwa.edu.au/~pbourke/geometry/lineline2d/

We can simplify things a bit before we start wrapping repetitive math operations up in variables:

Actionscript:
  1. nx = (p4.x - p3.x) * (p1.y - p3.y) - (p4.y - p3.y) * (p1.x - p3.x);
  2. // ny = (p2.x - p1.x) * (p1.y - p3.y) - (p2.y - p1.y) * (p1.x - p3.x);
  3. dn = (p4.y - p3.y) * (p2.x - p1.x) - (p4.x - p3.x) * (p2.y - p1.y);
  4. nx /= dn;
  5. // ny /= dn
  6. ny = p1.y + nx * (p2.y - p1.y);
  7. nx = p1.x + nx * (p2.x - p1.x);
  8. return new Point(nx, ny);

It's important to note here that if we didn't intend to calculate line segments we could exclude the initial calculation of ny.

At this point we're ready to get rid of duplicate math operations by wrapping them in variables. In this case this only includes subtraction:

Actionscript:
  1. var x4_x3:Number=p4.x-p3.x;
  2. var y4_y3:Number=p4.y-p3.y;
  3. var x2_x1:Number=p2.x-p1.x;
  4. var y2_y1:Number=p2.y-p1.y;
  5. var x1_x3:Number=p1.x-p3.x;
  6. var y1_y3:Number=p1.y-p3.y;
  7.  
  8. nx=x4_x3*y1_y3-y4_y3*x1_x3;
  9. ny=x2_x1*y1_y3-y2_y1*x1_x3;
  10. dn=y4_y3*x2_x1-x4_x3*y2_y1;
  11.  
  12. nx/=dn;
  13. ny/=dn;
  14.  
  15. ny=p1.y+nx*y2_y1;
  16. nx=p1.x+nx*x2_x1;
  17.  
  18. return new Point(nx, ny);

After wrapping repetitive math into variables the only optimization left to be made is related to the instantiation of a new Point(). Instantiation of objects is an expensive process, so it's better to define the point once outside of the intersection() function definition.

In the end our function looks like this:

Actionscript:
  1. var ip:Point = new Point();
  2.  
  3. function intersection(p1:Sprite, p2:Sprite, p3:Sprite, p4:Sprite):Point {
  4.     var nx:Number, ny:Number, dn:Number;
  5.     var x4_x3:Number = p4.x - p3.x;
  6.     var pre2:Number = p4.y - p3.y;
  7.     var pre3:Number = p2.x - p1.x;
  8.     var pre4:Number = p2.y - p1.y;
  9.     var pre5:Number = p1.y - p3.y;
  10.     var pre6:Number = p1.x - p3.x;
  11.     nx = x4_x3 * pre5 - pre2 * pre6;
  12.     ny = pre3 * pre5 - pre4 * pre6;
  13.     dn = pre2 * pre3 - x4_x3 * pre4;
  14.     nx /= dn;
  15.     ny /= dn;
  16.     // has intersection
  17.     if(nx>= 0 && nx <= 1 && ny>= 0 && ny <= 1){
  18.         ny = p1.y + nx * pre4;
  19.         nx = p1.x + nx * pre3;
  20.         ip.x = nx;
  21.         ip.y = ny;
  22.     }else{
  23.         // no intersection
  24.         ip.x = ip.y = -1000;
  25.     }
  26.     return ip
  27. }

The final version contains a conditional that checks the end values of nx and ny, if they are between 0 and 1 then we know that there is an intersection between the segments.

Note: Optimized code usually breaks some best practices... as best practices are usually geared toward code flexibility and readability... and not speed.

Also posted in Math | Tagged , | Leave a comment