Tag Archives: flash

Tweetcoding

Actionscript:
  1. g=graphics;
  2. mt=g.moveTo;
  3. lt=g.lineTo;
  4. ls=g.lineStyle;
  5. m=Math;
  6. r=m.random;
  7. s=m.sin;
  8. i=0;
  9. o={};
  10. function f(e){
  11.     s=150,x=y=s,z=-s,c=(!i)?addChild(new Bitmap(new BitmapData(s,s))).bitmapData:c;while(i<22500)i++,c.setPixel(i%s,i/s,(i%s|i/s)*mouseX);i=1;
  12. }
  13. addEventListener("enterFrame",f);

Have to link to this very fun new contest:

http://tweetcoding.machine501.com/

http://gskinner.com/playpen/tweetcoding.html

I may just have to get a twitter account...

Posted in misc | Also tagged | Leave a comment

24hr Clock

Actionscript:
  1. var clock:Sprite = Sprite(addChild(new Sprite()));
  2. clock.x = clock.y = 150;
  3.  
  4. var bg:Shape = Shape(clock.addChild(new Shape()));
  5. with (bg.graphics) lineStyle(2, 0x666666), beginFill(0xEFEFEF), drawCircle(0,0,110);
  6.  
  7. var hHand:Shape = clockHand(6, 50);
  8. var mHand:Shape = clockHand(2, 80);
  9. var sHand:Shape = clockHand(1, 90);
  10.  
  11. var center:Shape = Shape(clock.addChild(new Shape()));
  12. with (center.graphics) beginFill(0x000000), drawCircle(0,0,5);
  13.  
  14. var hInc:Number = 360/24;
  15. var msInc:Number = 360/60 ;
  16. var nOff:Number = 6;
  17. var verdana:TextFormat = new TextFormat("Verdana", 8);
  18. // add numbers to clock
  19. for (var i:int = 0; i<24; i++){
  20.      var ang:Number = (i * hInc - 90) * Math.PI/180;
  21.      createNumber(70,ang, i.toString());
  22.      var ms:Number = i * 2.5;
  23.      if (ms % 5 == 0){
  24.         createNumber(95, ang, ms.toString());
  25.      }
  26. }
  27.  
  28. addEventListener(Event.ENTER_FRAME, onLoop);
  29. function onLoop(evt:Event):void {
  30.     var date:Date = new Date();
  31.     hHand.rotation =  hInc * date.getHours();
  32.     mHand.rotation =  msInc * date.getMinutes();
  33.     sHand.rotation =  msInc * date.getSeconds();
  34. }
  35.  
  36. function clockHand(thickness:Number, leng:Number):Shape{
  37.     var hand:Shape = Shape(clock.addChild(new Shape()));
  38.     with (hand.graphics) {
  39.         lineStyle(thickness, 0x000000, 1, true, LineScaleMode.NORMAL, CapsStyle.SQUARE);
  40.         lineTo(0,-leng);
  41.     }
  42.     return hand;
  43. }
  44.  
  45. function createNumber(radius:Number, theta:Number, str:String):void{
  46.     var t:TextField = TextField(clock.addChild(new TextField()));
  47.     with (t) defaultTextFormat = verdana, t.autoSize = "left";
  48.     t.text = str;
  49.     t.x = radius * Math.cos(theta) - nOff;
  50.     t.y = radius* Math.sin(theta) - nOff;
  51. }

I was watching a movie the other day and I saw a 24 hour clock in the background of one of the shots. After the movie I coded this snippet. It draws a very basic clock with an hour hand that takes 24 hours to go full circle...

Posted in misc | Also tagged | 4 Comments

Square Bracket Syntax & Typecasting

Actionscript:
  1. addChild(new TextField());
  2. getChildAt(0)["text"] = "Hello World";

Notice that square bracket syntax makes it so we don't need to typecast. Take a look at the other way:

Actionscript:
  1. addChild(new TextField());
  2. TextField(getChildAt(0)).text = "Hello World";

Typecasting is the way to go.... the square bracket technique is just an interesting trick.

I wrote this after somehow stumbling on this very entertaining page - it shows hello world written in approximately 200 different languages...

Posted in dynamic, misc | Also tagged | Leave a comment

Snap to Grid

Actionscript:
  1. [SWF(width = 600, height = 400)]
  2.  
  3. // draw the same grid as yesterday
  4. var tileSize:int = 40;
  5. var cols:int = stage.stageWidth / tileSize;
  6. var rows:int = stage.stageHeight / tileSize;
  7. var grid:Sprite = Sprite(addChild(new Sprite()));
  8. grid.graphics.lineStyle(0,0x000000);
  9. var i:int = 0;
  10. for (i = 1; i<cols; i++){
  11.     var posX:Number = i * tileSize
  12.     grid.graphics.moveTo(posX, 0);
  13.     grid.graphics.lineTo(posX, stage.stageHeight);
  14. }
  15. for (i = 1; i<rows; i++){
  16.     var posY:Number = i * tileSize
  17.     grid.graphics.moveTo(0, posY);
  18.     grid.graphics.lineTo(stage.stageWidth, posY);
  19. }
  20.  
  21. //
  22. // -- add a circle that snaps to the grid when dragged
  23. //
  24. var circle:Sprite = Sprite(addChild(new Sprite()));
  25. with (circle.graphics) beginFill(0xFF0000), drawCircle(0,0,10);
  26. circle.x = circle.y =  tileSize * 3;
  27. circle.buttonMode = true;
  28.  
  29. circle.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
  30. function onDown(evt:MouseEvent):void {
  31.     addEventListener(Event.ENTER_FRAME, onRunSnapping);
  32. }
  33.  
  34. function onRunSnapping(evt:Event):void {
  35.     circle.x =  Math.round(mouseX / tileSize) * tileSize;
  36.     circle.y =  Math.round(mouseY / tileSize) * tileSize;
  37. }
  38.  
  39. stage.addEventListener(MouseEvent.MOUSE_UP, onUp);
  40. function onUp(evt:MouseEvent):void {
  41.     removeEventListener(Event.ENTER_FRAME, onRunSnapping);
  42. }

This builds on yesterdays post by adding a draggable red circle that snaps to the grid. This is the real trick:

Actionscript:
  1. circle.x =  Math.round(mouseX / tileSize) * tileSize;
  2. circle.y =  Math.round(mouseY / tileSize) * tileSize;

Posted in Graphics, UI, motion | Also tagged | Leave a comment