Monthly Archives: November 2008

MovieClip.prototype

Actionscript:
  1. MovieClip.prototype.tint = function(col:uint):void {
  2.     var ct:ColorTransform = transform.colorTransform;
  3.     ct.color = col;
  4.     this.transform.colorTransform = ct;
  5. }
  6.  
  7. var circle:MovieClip = MovieClip(addChild(new MovieClip()));
  8. with (circle.graphics) {
  9.     beginFill(0x123455);
  10.     drawCircle(0,0,50);
  11.     x = 100;
  12.     y = 100;
  13. }
  14.  
  15. circle.tint(0xFF0000);

When AS3 first came out I didn't realize that prototype was still around.... This adds a function called tint() to all MovieClips. You should extend MovieClip instead of using this method.... but it's interesting to see that it's still around. There's an explanation of prototype and the AS3 namespace here.

Posted in MovieClip, color | Tagged , , , | Leave a comment

10,000 Transparent Sprites

Actionscript:
  1. stage.frameRate = 30;
  2.  
  3. var imageNum:int = 10000;
  4. var point:Point = new Point(0,0);
  5. var s:Sprite = new Sprite();
  6. s.graphics.beginFill(0xCCCCCC);
  7. s.graphics.lineStyle(0,0x000000);
  8. s.graphics.drawCircle(3,3,3);
  9. s.alpha = .1;
  10.  
  11. var nested:Sprite = new Sprite();
  12. nested.addChild(s);
  13. var image:BitmapData = new BitmapData(s.width, s.height, true, 0x00000000);
  14. image.draw(nested);
  15.  
  16. var canvas:BitmapData = new BitmapData(400,400, true, 0xFFFFFFFF);
  17. addChild(new Bitmap(canvas));
  18.  
  19. var xPos:Array = new Array();
  20. var yPos:Array = new Array();
  21. for (var i:int = 0; i<imageNum; i++) {
  22.     xPos.push(Math.random()*400);
  23.     yPos.push(Math.random()*400);
  24. }
  25.  
  26. addEventListener(Event.ENTER_FRAME, onLoop);
  27. function onLoop(evt:Event):void {
  28.     canvas.fillRect(new Rectangle(0,0,400,400), 0xFFFFFFFF);
  29.     var div:Number;
  30.     for (var i:int = 0; i<imageNum; i++) {
  31.         div  = (i / 100)+2;
  32.         xPos[i] += (mouseX - xPos[i])/div;
  33.         yPos[i] += (mouseY - yPos[i])/div;
  34.         point.x = xPos[i];
  35.         point.y = yPos[i];
  36.         canvas.copyPixels(image, image.rect, point, null, null, true);
  37.     }
  38. }

This is from my other blog but I figured it was worth posting here. It draws 10'000 transparent circles that each follow the mouse at different speeds. This is achieved using copyPixels().

Posted in BitmapData, motion | Tagged , , | 2 Comments

Loader, Centering, Smoothing

Actionscript:
  1. var image:Loader = new Loader();
  2. image.load(new URLRequest("http://www.shapevent.com/sketchbook/wp-content/uploads/avalanche.jpg"));
  3. image.x = stage.stageWidth / 2;
  4. image.y = stage.stageHeight / 2;
  5. addChild(image);
  6.  
  7. image.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
  8. function onComplete(evt:Event):void {
  9.    
  10.     // CENTER THE IMAGE
  11.     image.content.x = -image.content.width / 2;
  12.     image.content.y = -image.content.height / 2;
  13.    
  14.     // BITMAP SMOOTHING
  15.     Bitmap(image.content).smoothing = true;
  16.     image.contentLoaderInfo.removeEventListener(Event.COMPLETE, onComplete);
  17. }
  18.  
  19.  
  20. // scale the image with the mouse to demonstrate smoothing and scaling from the center
  21. addEventListener(Event.ENTER_FRAME, onLoop);
  22. function onLoop(evt:Event):void {
  23.     image.scaleX = image.scaleY = mouseX / stage.stageWidth;
  24. }

This is really two snippets combined. How to scale the content of a Loader from the center:

Actionscript:
  1. // CENTER THE IMAGE
  2. image.content.x = -image.content.width / 2;
  3. image.content.y = -image.content.height / 2;

and how to smooth a jpg, png etc... that has been loaded dynamically into your swf:

Actionscript:
  1. // BITMAP SMOOTHING
  2. Bitmap(image.content).smoothing = true;

Posted in Uncategorized | 3 Comments

Reset Registration Point

Actionscript:
  1. // display object target, value - "center" or "upperLeft"
  2. function setRegistration(dsp:DisplayObjectContainer, v:String):void {
  3.     var i:int;
  4.     var child:DisplayObject;
  5.     var b:Rectangle=getBounds(dsp);
  6.     for (i = 0; i <dsp.numChildren; i++) {
  7.         child=dsp.getChildAt(i);
  8.         child.x+=b.x*-1;
  9.         child.y+=b.y*-1;
  10.     }
  11.     if (v=="center") {
  12.         dsp.x+=dsp.width/2;
  13.         dsp.y+=dsp.height/2;
  14.         for (i = 0; i <dsp.numChildren; i++) {
  15.             child=dsp.getChildAt(i);
  16.             child.x-=dsp.width/2;
  17.             child.y-=dsp.height/2;
  18.         }
  19.     } else if (v == "upperLeft") {
  20.         if (dsp.parent) {
  21.             b=getBounds(dsp.parent);
  22.             dsp.x=b.left;
  23.             dsp.y=b.top;
  24.         }
  25.     }
  26. }
  27.  
  28. // example:
  29. // (make a MovieClip and fill it with, text, shapes etc...)
  30.  
  31. setRegistration(clip, "center");
  32. // registration(clip, "upperLeft");
  33.  
  34. addEventListener(Event.ENTER_FRAME, onLoop);
  35. function onLoop(evt:Event):void {
  36.     clip.rotation+=1;
  37. }

Teaching beginner flash... I've noticed people always get confused about the registration point. They think they should be able to move it with the IDE or ActionScript. I always say "You can't change the registration point, you can only change the position of graphical elements in relationship to it." Anyway, as a sort of joke one day after class I wrote the above function... it changes the registration of any DisplayObjectContainer to either "center" or "upperLeft".

To test this code out, fill a MovieClip with a bunch of different things (text, graphics, scribbles)... give it a strange registration point and then run the above code on it. You'll be able to tell that the registration has changed by the way the clip rotates. Although I haven't been able to break this function, I think it's possible that it doesn't work under all circumstances.

Using reparenting is a more elegant solution to this issue. Maybe I'll post that in the next couple days.

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