Category Archives: misc

XOR Conditional

Actionscript:
  1. addEventListener(Event.ENTER_FRAME, onLoop);
  2.  
  3. function onLoop(evt:Event):void {
  4.     var boolA:Boolean = false;
  5.     var boolB:Boolean = false;
  6.    
  7.     if (mouseX> 200){
  8.         boolA = true;
  9.     }else{
  10.         boolA = false;
  11.     }
  12.    
  13.     if (mouseY <200){
  14.         boolB = true
  15.     }else{
  16.         boolB = false;
  17.     }
  18.    
  19.     if (boolA || boolB){
  20.         trace("regular or");
  21.     }
  22.    
  23. // this is the XOR conditional
  24.     if (int(boolA) ^ int(boolB)){
  25.         trace("exclusive or");
  26.     }
  27.    
  28. }

There's an obvious more logical ways to do this. But I thought it was somewhat interesting to see....

So in binary XOR will give us:
0^0 = 0;
0^1 = 1;
1^0 = 1;
1^1 = 0;

What other operator might you use to get the same results?... that's right !=

With regular OR we get:
0|0 = 0;
0|1 = 1;
1|0 = 1;
1|1 = 1;

Posted in misc | Tagged , , , , | Leave a comment

Unique ID

Actionscript:
  1. trace((new Date()).getTime() + Math.random()*0xFFFFFF);

A hacky way to get a unique ID, although this isn't, perfect it works well enough. For a project that you really expect to get huge amounts of traffic... you might consider using php's uniqid() function and passing it into your swf. That said, the odds of two users getting the same two ID values are about as good as the odds of you winning the lottery twice in a row.

Here is a version wrapped in a function, with a prefix argument:

Actionscript:
  1. trace(uniqid("z"));
  2.  
  3. function uniqid(prefix:String):String{
  4. return prefix + (new Date()).getTime() + Math.random()*0xFFFFFF;
  5. }

Also posted in one-liners, random | Tagged , | Leave a comment

25 Lines Contest

Actionscript:
  1. for (var i:int = 1; i<26; i++) with(addChild(new TextField())) x = 10, y = i * 10, text = "line" + i;

code will create 25 TextFields that say "line1", "line2", "line3" etc...

If you don't already know, Keith Peters has started up the 25 line actionscript contest again... The first round of results are in and you can view the 12 finalists swfs and code here.

I was particularly impressed with this one...by Cay Garrido... when I looked at it I really couldn't figure out how that kind of seemingly complex flocking could be done with 25 lines. You can see the code here.

Inspired by the entries, Peters created a strange attractor in 6 lines.

I can't wait to see next months entries.

Posted in misc | Tagged , | Leave a comment