Tag Archives: actionscript

ByteArray.readUTFBytes()

Actionscript:
  1. var m:ByteArray = new ByteArray();
  2. var bytes:Array = [0x41, 0x63, 0x74, 0x69, 0x6F, 0x6E, 0x20,
  3.                    0x53, 0x6E, 0x69, 0x70, 0x70, 0x65, 0x74]
  4. for (var i:int = 0; i <bytes.length; i++){
  5.     m.writeByte(bytes[i]);
  6. }
  7.  
  8. m.position = 0;
  9.  
  10. trace(m.readUTFBytes(bytes.length));

I was messing around with a hex editor today and decided to start playing with ByteArray for the first time in awhile. Just wrote this as a little warm up... Try running it to see what it traces out...

Posted in Uncategorized | Also tagged , , | 2 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....

Posted in Math, Operators, binary, misc | Also tagged , , | 1 Comment

Bad PI Approximation

Actionscript:
  1. var inside:Number = 0
  2. var precision:Number = 1000000;
  3. for (var i:int = 0; i<precision; i++){
  4.        var xp:Number = 0.5 - Math.random();
  5.        var yp:Number = 0.5 - Math.random();
  6.        if (Math.sqrt(xp * xp + yp * yp) <0.5){
  7.                inside++;
  8.        }
  9. }
  10. trace(inside / precision * 4);
  11. // outputs : 3.143304

Someone described this bad method of a PI approximation to me the other day... figured I'd try it out... pretty funny. I always liked 22/7... but this one is definitely funnier...

Posted in Math | Also tagged , | 3 Comments

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 | Also tagged , | 3 Comments