Category Archives: misc

Flowing Leaves (optical illusion)


Saw this optical illusion today... figured I'd make a snippet to create a few variations on the illusion...

Actionscript:
  1. [SWF( backgroundColor=0x2E7999, width=780, height = 600) ]
  2.  
  3. var leafNum:Number = 375;
  4. var spacing:Number = 12;
  5. var cols:Number = 25;
  6. var hh:Number = stage.stageHeight / 2;
  7. var hw:Number = stage.stageWidth / 2;
  8.  
  9. for (var i:Number = 0; i<leafNum; i++){
  10.     var leaf:Shape = makeLeaf();
  11.     leaf.scaleX = leaf.scaleY = 0.25;
  12.     leaf.rotation = 90;
  13.     leaf.x = 50 + (i % cols) * (leaf.width + spacing);
  14.     leaf.y = 40 + int(i / cols) * (leaf.height + spacing);
  15.     var dx:Number = leaf.x - hw;
  16.     var dy:Number = leaf.y - hh;
  17.     leaf.rotation = Math.sqrt(dx * dx + dy * dy);
  18. }
  19.  
  20. function makeLeaf():Shape{
  21.     var leaf:Shape = Shape(addChild(new Shape()));
  22.     leaf.graphics.beginFill(0x9DC4D4);
  23.     scaleYcircle(leaf.graphics, 50, .65, false);
  24.     leaf.graphics.endFill();
  25.     leaf.graphics.lineStyle(2, 0x003366, 1, false, "none", CapsStyle.SQUARE, JointStyle.MITER);
  26.     scaleYcircle(leaf.graphics, 50, .65);
  27.     leaf.graphics.lineStyle(2, 0xFFFFFF, 1, false, "none", CapsStyle.SQUARE, JointStyle.MITER);
  28.     scaleYcircle(leaf.graphics, -50, .65);
  29.     return leaf;
  30. }
  31.  
  32.  
  33. // original circle function by senocular (www.senocular.com) from here http://www.actionscript.org/forums/showthread.php3?s=&threadid=30328
  34. // circle that can be scaled on the y axis
  35. function scaleYcircle(g:Graphics, r:Number, s:Number = 1, isHalf:Boolean=true):void {
  36.      
  37.     var c1:Number = r * (Math.SQRT2 - 1);
  38.     var c2:Number = r * Math.SQRT2 / 2;
  39.     var rs:Number = r * s, c1s:Number = c1 * s, c2s:Number = c2 * s;
  40.     var x_r:Number =  -r, y_r:Number = -rs, x_c2:Number =  -c2;
  41.     var y_c2:Number =  -c2s, x_c1:Number =  -c1, y_c1:Number =  -c1s
  42.     g.moveTo(r, 0), g.curveTo(r, c1s, c2, c2s);
  43.     g.curveTo(c1, rs, 0, rs), g.curveTo(x_c1,rs, x_c2, c2s);
  44.     g.curveTo(x_r, c1s, x_r, 0);
  45.     if (!isHalf){
  46.      g.curveTo(x_r,y_c1,x_c2,y_c2);
  47.      g.curveTo(x_c1,y_r,0,y_r), g.curveTo(c1,y_r,c2,y_c2);
  48.      g.curveTo(r,y_c1,r,0);
  49.     }
  50. }

Also posted in Graphics, Math, Vector, motion, pixel manipulation | Tagged , , , | 5 Comments

Bitwise OR | and Variable Function Arguments (bitwise flags)

Actionscript:
  1. const A:uint = 1;
  2. const B:uint = 2;
  3. const C:uint = 4;
  4. const D:uint = 8;
  5. const E:uint = 16;
  6.  
  7. function orArgs(args:uint):void{
  8.     if (args & A){
  9.         trace("A");
  10.     }
  11.     if (args & B){
  12.         trace("B");
  13.     }
  14.     if (args & C){
  15.         trace("C");
  16.     }
  17.     if (args & D){
  18.         trace("D");
  19.     }
  20.     if (args & E){
  21.         trace("E");
  22.     }
  23. }
  24.  
  25. // test out the function:
  26. orArgs(A | B);
  27. trace("--");
  28. orArgs(A | C | E);
  29. trace("--");
  30. orArgs(B | E | D);
  31. trace("--");
  32. orArgs(C | A);
  33.  
  34. /* outputs:
  35. A
  36. B
  37. --
  38. A
  39. C
  40. E
  41. --
  42. B
  43. D
  44. E
  45. --
  46. A
  47. C
  48. */

If you've every used Array.sort(Array.NUMERIC | Array.DESCENDING) you should have at least a vague idea about what this snippet is doing. It shows how you can pass a variable number of arguments to a function using | (bitwise OR) and & (bitwise AND). I believe the correct term for these kind of arguments is "bitwise flags". This snippet works by having a series of constant values... in this case A - E. Each constant is assigned an unsigned integer... now you may not see the significance of the values 1, 2, 4, 8 and 16 until you see them in binary... get ready this is a pretty cryptic description...

A = 00001 = 1
B = 00010 = 2
C = 00100 = 4
D = 01000 = 8
E = 10000 = 16

If we OR all these together we get: 11111... If we do:

A | E
00001 | 10000

we end up with 10001...

...we can then check which values are stored in the resulting unsigned integer by using AND:

check for A... 10001 & 00001 = 00001 = true
check for E... 10001 & 10000 = 10000 = true
check for C... 10001 & 00100 = 00000 = false

That's it... I just guessed at the way this was being done... if you have another way to do the same thing, feel free to post it in the comments....

Also posted in Math, Operators, binary | Tagged , , , | 1 Comment

Project Time-bomb

Actionscript:
  1. var d:Date = new Date()
  2. // payment deadline
  3. var payMonth:int = 9;
  4. var payDay:int = 5;
  5. // add five days so it's not too obvious and so you can replace
  6. // the swf on their server if they do pay
  7. payDay+=5;
  8. if (d.getMonth()>= payMonth){
  9.     if (d.getDay()>= payDay){
  10.         // very nasty code to crash flash, alternately you could do anything that will break
  11.         // you app
  12.         while(1){
  13.            stage.addEventListener(Event.ENTER_FRAME, function(){ this["__"+Math.random()]=getTimer()*Math.random()});
  14.         }
  15.     }
  16. }

WARNING: This snippet and post are a JOKE. Be careful, this code could potentially cause flash a 15 second timeout in flash and will eat ram and cpu.

Have you ever had to bug a client to get paid when the site has already gone live? Sometimes the agreement you have with the client could prevent you from getting paid until 30 or even 90 days after the site is live. I generally try to avoid these types of agreements and get a chunk of 30-50% upfront, but every now and then the job is too good to pass up and I make the choice to just suck it up and wait to get paid.... anyway...

This snippet adds a time-bomb to your swf... if the client doesn't pay up... five days after the payment deadline the swf will stop working. They will be forced to contact you and you can say that they need to pay before you'll do any additional work. You can say you don't know what the problem is off the top of your head, you'll need to go in and take a look. You can say that your local version works... so maybe the online file was corrupted... but that you'll need to be paid before you can do anything else.

I'm just kidding around. Luckily, out of all the freelance jobs I've done over the years there was only one time when I didn't get paid in full...

Posted in misc | Tagged , , | 3 Comments

Is this swf in a container?

Actionscript:
  1. if(parent == stage){
  2.    // no container
  3. }else{
  4.   // has container
  5. }

Found myself writing this snippet today.... it figures out if the current swf is inside a container (Loader) or not... lots of other ways to do this... feel free to post your version in the comments...

Also posted in Loader, external data | Tagged , , | 5 Comments