Monthly Archives: March 2009

Animate Graphics.lineTo()

Actionscript:
  1. // line thickness, line color, start point, end point, divisor, callback
  2. function animateLineTo( thick:Number, col:uint, sp:Point, ep:Point, div:Number=4, callback:Function=null):void {
  3.      var s:Shape  = Shape(addChild(new Shape()));
  4.      var ap:Point = sp.clone();
  5.      div = Math.max(2, div);
  6.      setTimeout(runLineAnimation, 1000/stage.frameRate, s.graphics , thick , col , sp , ep , ap, div, callback);
  7. }
  8.  
  9. function runLineAnimation(g:Graphics , thick:Number, col:uint, sp:Point, ep:Point, ap:Point, div:Number, callback:Function):void {
  10.      ap.x += (ep.x - ap.x) / div;
  11.      ap.y += (ep.y - ap.y) / div;
  12.       with(g){
  13.         clear();
  14.         lineStyle(thick, col);
  15.         moveTo(sp.x, sp.y);
  16.         lineTo(ap.x, ap.y);
  17.       }
  18.        if (Math.abs(ap.x - ep.x) <1 && Math.abs(ap.y- ep.y)  <1){
  19.            // done
  20.            if (callback!=null){
  21.                callback();  
  22.            }
  23.        }else{
  24.             setTimeout(runLineAnimation, 1000/stage.frameRate, g , thick , col , sp , ep , ap, div, callback);
  25.        }
  26. }
  27. //
  28. // test out the animateLineTo function:
  29. //
  30. var a:Point = new Point(100,100);
  31. var b:Point = new Point(150, 200);
  32. var c:Point = new Point(300, 190);
  33. var d:Point = new Point(280, 90)
  34.  
  35. animateLineTo(0, 0xFF0000, a, b, 4, line2);
  36.  
  37. function line2():void{
  38.     animateLineTo(0, 0xFF0000, b, c, 4, line3);
  39. }
  40.  
  41. function line3():void{
  42.     animateLineTo(0, 0xFF0000, c, d, 4, line4);
  43. }
  44.  
  45. function line4():void{
  46.     animateLineTo(0, 0xFF0000, d, a, 4);
  47. }

The above demos a function called animateLineTo() that will draw a line using zeno's paradox.

There are a couple weird/interesting tricks going on here. I'm using setTimeout() over and over rathaer than using setInterval(). This makes it so I don't need to store an interval id. I'm also using a delay of 1000/stage.frameRate, this attempts to cause the setTimeout() to run at the same frequency as an enter frame would. This is important because having a setTimeout()/setInterval() that is doing animation and attempting to run more frequently than the framerate can cause problems and is a waste of cpu power.

This snippet was spurned by a student question about how to animate a Graphics.lineTo() from point A to point B. Here is the original snippet, which is easier to read and more bare bones:

Actionscript:
  1. var p0:Point = new Point(100,100);
  2. var p1:Point = new Point(400,200);
  3. var dp:Point = new Point(p0.x,p0.y);
  4.  
  5. var s:Sprite = Sprite(addChild(new Sprite()));
  6.  
  7. addEventListener(Event.ENTER_FRAME, onLoop);
  8. function onLoop(evt:Event):void {
  9.     s.graphics.clear()
  10.     s.graphics.lineStyle(0,0x000000);
  11.     s.graphics.moveTo(p0.x, p0.y);
  12.     s.graphics.lineTo(dp.x, dp.y);
  13.     dp.x += (p1.x - dp.x) / 4;
  14.     dp.y += (p1.y - dp.y) / 4;
  15. }

Posted in motion | Tagged , | 1 Comment

Readable Conditionals

Actionscript:
  1. // NOTE: this code won't do anything in you're timline....
  2.  
  3. // do you do this?
  4.  
  5. if (ball.x <0 || ball.y <0 || ball.x> stage.stageWidth || ball.y> stage.stageHeight){
  6.        // cause ball to explode
  7. }
  8.  
  9. // or this?
  10.  
  11. if (isAtStageEdge(ball)){
  12.        // cause ball to explode
  13. }
  14.  
  15. function isAtStageEdge(mc:MovieClip):Boolean{
  16.    return (mc.x <0 || mc.y <0 || mc.x> stage.stageWidth || mc.y> stage.stageHeight);
  17. }

This is pretty standard stuff, but it can increase code readability especially for complex conditionals.

I also do this if I find myself repeating an a semi-complex if statement in two or more places.

If I'm just brainstorming I probably won't bother, but on real projects it helps keep code readable.

Posted in functions | Tagged , | Leave a comment

String.fromCharCode()

Actionscript:
  1. [SWF(height = 950, width=1000)]
  2. var txt:TextField;
  3. var num:int = 0;
  4. for (var i:int = 0; i<761; i++){
  5.     if (i % 76 == 0){
  6.          makeTxt(num);
  7.          num++;
  8.     }
  9.     if (i <0x21){
  10.         txt.appendText("-\n");
  11.     }else{
  12.         txt.appendText(String.fromCharCode(i) + " = " +  i + "\n");
  13.     }
  14. }
  15. function makeTxt(num:Number):void{
  16.      txt= TextField(addChild(new TextField()));
  17.      txt.x = 10 + num * 100;
  18.      txt.y = 10;
  19.      txt.height = 930;
  20.      txt.text = "";
  21. }

This will draw a few columns of char codes.

Posted in strings | Tagged , | Leave a comment