Monthly Archives: December 2008

FileReference.save()

Actionscript:
  1. var file:FileReference = new FileReference();
  2.  
  3. stage.addEventListener(MouseEvent.CLICK, onClick);
  4.  
  5. function onClick(evt:MouseEvent):void {
  6.     file.save("some text. \nsome more text", "actionsnippet.txt");
  7. }

This is possibly my favorite feature of flash 10. Save any kind of file to the users computer...

The first argument is for the data to put in the file, this can be a String, ByteArray or XML object. The second argument is the name of the file.

As a test I also created one that saved a BitmapData object as a jpeg. But it doesn't really fall into the category of snippet because it uses the Jpeg Encoder from adobe.

Posted in misc | Tagged , | 6 Comments

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. }

Posted in misc, one-liners, random | Tagged , | Leave a comment

XML Node Parents

Actionscript:
  1. function numParents(e:XML):int {
  2.     var num:int = 0;
  3.     while (e.parent()!= null) {
  4.         num++;
  5.         e = e.parent();
  6.     }
  7.     return num;
  8. }

Sometimes when reading through XML it's helpful to know how many parents a given node has. This function will do that. I recently used this function in an accordion widget.

Posted in XML | Tagged , | Leave a comment