Tag Archives: flash

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 | Also 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 | Also tagged | Leave a comment

Web Cam

Actionscript:
  1. var cam:Camera =  Camera.getCamera();
  2. var video:Video = new Video(320, 240);
  3. video.attachCamera(cam);
  4. addChild(video);

Adds the feed from your webcam to the stage.

Posted in Video | Also tagged | 1 Comment

BitmapData.setVector()

Actionscript:
  1. var canvas:BitmapData=new BitmapData(255,255,false,0x000000);
  2. addChild(new Bitmap(canvas, "auto", true));
  3.  
  4. var size:int = canvas.width * canvas.height;
  5.  
  6. var pixels:Vector.<uint> = new Vector.<uint>(size);
  7.  
  8. for (var i:int  = 0; i<size; i++) {
  9.     var ox:uint= i % canvas.width;
  10.     var oy:uint= i / canvas.width;
  11.     pixels[i] = oy <<16 | ox;
  12. }
  13.    
  14. canvas.setVector(canvas.rect, pixels);

setVector() is used to set a group of pixels. I've been wanting this feature for a long time. I'm assuming that it's at least a little faster than using a bunch of setPixel() calls, but i haven't tested it. Of course, Pixel Bender will be much faster than this.... as long as your not on a powerPC based mac (or some other older computer) ... as you may or may not know Pixel Bender will run very slow on a powerPC mac:

Quote from Tinic Uro - Flash Player Engineer

But... I have more news you might not like. ;-) If you ever run a Pixel Bender filter on PowerPC based Mac you will see that it runs about 10 times slower than on an Intel based Mac. For this release we only had time to implement a JIT code engine for Intel based CPUs. On a PowerPC Mac Pixel Bender kernels will run in interpreted mode. I leave it up to you to make a judgment of how this will affect you. All I can say: Be careful when deploying content using Pixel Bender filters, know your viewers.

Read the rest of the article here.

Posted in BitmapData, Vector, pixel manipulation, setPixel | Also tagged | Leave a comment