Monthly Archives: December 2008

Bring Display Object to Top

Actionscript:
  1. mc.addEventListener(MouseEvent.ROLL_OVER, onRollOver);
  2. function onRollOver(evt:MouseEvent):void {
  3.   addChild(MovieClip(evt.currentTarget));
  4. }

This one is very simple, but it's important to note that using addChild() on something that is already on the display list simply brings it to the top. Back in AS2 we used to do:

Actionscript:
  1. mc.swapDepths(1000);

Posted in DisplayObject, display list | Tagged , | 3 Comments

Procedural Pattern - Navigation

Actionscript:
  1. var currentBtn:MovieClip;
  2.  
  3. var nav:Sprite = new Sprite();
  4. nav.x = nav.y = 20;
  5. addChild(nav);
  6.  
  7. createBtns();
  8.  
  9. nav.addEventListener(MouseEvent.CLICK, onClickBtn);
  10.  
  11. function createBtns():void{
  12.     for (var i:int = 0; i<10; i++){
  13.         var btn:MovieClip = new MovieClip();
  14.         with(btn.graphics) beginFill(0x666666), drawRect(-10,-10,20,20);
  15.         btn.x = i * (btn.width + 10);
  16.         btn.buttonMode = true;
  17.         btn.num = i;
  18.         btn.alpha = .5;
  19.         nav.addChild(btn);
  20.     }
  21. }
  22.  
  23. function onClickBtn(evt:MouseEvent):void {
  24.     //
  25.     // this is the important part
  26.     //
  27.     if (currentBtn){
  28.         currentBtn.scaleX = currentBtn.scaleY = 1;
  29.         currentBtn.alpha = .5;
  30.     }
  31.     currentBtn = MovieClip(evt.target);
  32.     currentBtn.scaleX = currentBtn.scaleY = 1.3;
  33.     currentBtn.alpha = 1;
  34.     trace("current button:", currentBtn.num);
  35.     //
  36. }

This code will create 10 boxes that represent buttons on a navigation. When you click a box it indicates that it is selected by scaling up and changing alpha. Simple enough...

The term Procedural Pattern is just a spin on the idea of Design Patterns. I've come up with lots of small patterns to solve simple recurring problems over the years. Most of these relate to things like drawing programs, ecards and games.

Posted in UI | Tagged , , | Leave a comment

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

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 | 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 | Tagged , | Leave a comment

Hidden Sierpiński

Actionscript:
  1. var canvas:BitmapData=new BitmapData(255,255,false,0x000000);
  2. addChild(new Bitmap(canvas, "auto", true));
  3.  
  4. scaleX = scaleY = 1.5;
  5.  
  6. addEventListener(Event.ENTER_FRAME, onLoop);
  7. function onLoop(evt:Event):void {
  8.     for (var i:int  = 0; i<canvas.width * canvas.height; i++) {
  9.         var ox:int= i % canvas.width;
  10.         var oy:int= i / canvas.width;
  11.         var col =  (ox | oy) * mouseX % 255;
  12.         canvas.setPixel(ox, oy, col <<16 | col <<8 | col);
  13.     }
  14. }

Sometimes when I'm writing a program that does some pixel pushing I'll save the file and then start arbitrarily adding bitwise operations to it.... just to see what happens. Sierpinski-esque stuff occurs often. This will work in your timeline - move your mouse left and right to generate images like these:





Posted in BitmapData, pixel manipulation, setPixel | Tagged , , | Leave a comment

Texture cos(atan2())

Actionscript:
  1. var col:int, i:int, j:int, s:int = 500, div:Number =20, outcoord:Point = new Point(), points:Vector.<Point> = new Vector.<Point>();
  2. for (i = 0; i<5; i++) points.push(new Point(int(Math.random()*s),int(Math.random()*s)));
  3. var canvas:Bitmap = Bitmap(addChild(new Bitmap(new BitmapData(s,s, false, 0xFF0000), "auto", true)));
  4. for (i  = 0; i<canvas.width * canvas.height; i++){
  5.     outcoord= new Point( i % canvas.width, i / canvas.width);
  6.     col = 0;
  7.     for (j= 0; j<points.length; j++) col += Math.max(0,255 * Math.cos(Math.atan2(outcoord.y - points[j].y, outcoord.x - points[j].x)*outcoord.x/div) );
  8.     col /= points.length;
  9.     canvas.bitmapData.setPixel(outcoord.x, outcoord.y,  col <<16 |  col <<8 |  col);
  10. }

This is inspired by some stuff I've been doing with PixelBender. I think I'm going to convert this to processing to see how fast I can get it to run... will post that over at shapevent. People have been asking me to post swfs.... and I plan on starting to that a little bit... but since this one doesn't animate, here are a few jpgs:

Posted in BitmapData, pixel manipulation, setPixel | Tagged , | Leave a comment