Category Archives: functions

Object.constructor()

Actionscript:
  1. var Box:Function = {
  2.     constructor : function(color:uint, w:Number, h:Number):void {
  3.         this.color = color;
  4.         this.s = new Shape();
  5.         with(this.s.graphics) beginFill(this.color), drawRect(0,0,w,h);
  6.         addChild(this.s);
  7.        
  8.         this.setLoc = function(x:Number, y:Number):void{
  9.             this.s.x = x;
  10.             this.s.y = y;
  11.        }
  12.     }
  13. }.constructor;
  14.  
  15. var box0:Object = new Box(0xFF0000, 100, 100);
  16.  
  17. box0.setLoc(100, 10);
  18.  
  19. var box1:Object = new Box(0x000000, 50, 50);
  20.  
  21. box1.setLoc(210, 10);

This snippet makes use of Object.constructor() to allow creation of Object instances using the new keyword. This is for fun only, I don't recommend this over actual Classes.

Also posted in OOP, dynamic, misc | Tagged , | Leave a comment

Circle of Particles

Actionscript:
  1. var canvas:BitmapData = new BitmapData(400,400, false, 0xFFFFFF);
  2. addChild(new Bitmap(canvas));
  3. var eraser:BitmapData = new BitmapData(400,400, true, 0x33FFFFFF);
  4.  
  5. var particles:Array = new Array();
  6. for (var i:int = 0; i <1000; i++){
  7.     particles.push(makeParticle());
  8. }
  9.  
  10. addEventListener(Event.ENTER_FRAME, onLoop);
  11. function onLoop(evt:Event):void {
  12.     canvas.copyPixels(eraser, eraser.rect, new Point(0,0), null, null, true);
  13.     for (var i:int = 0; i <particles.length; i++){
  14.         particles[i]();
  15.     }
  16. }
  17.  
  18. function makeParticle():Function {
  19.     var dx:Number, dy:Number;
  20.     var x:Number = 200;
  21.     var y:Number = 200;
  22.     var vx:Number = Math.random() * 4 - 2;
  23.     var vy:Number = Math.random() * 4 - 2;
  24.     return function():void {
  25.               x += vx;
  26.               y += vy;
  27.               dx = x - 200;
  28.               dy= y - 200;
  29.                if (Math.sqrt((dx * dx) + (dy * dy))> 100){
  30.                  vx *= -1;
  31.                  vy *= -1;
  32.                }
  33.                canvas.setPixel(x, y, 0x000000);
  34.     }
  35. }

This creates a circular area filled with moving particles/pixels. It makes use of yesterdays functional programming techniques.

Also posted in BitmapData, motion, setPixel | Tagged , | Leave a comment

Functional Pseudo-Objects

Actionscript:
  1. var moverNum:int = 40;
  2. var movers:Array = new Array();
  3.  
  4. for (var i:int = 0; i<moverNum; i++){
  5.     movers.push(makeMover());
  6. }
  7.  
  8. addEventListener(Event.ENTER_FRAME, onLoop);
  9. function onLoop(evt:Event):void {
  10.     for (var i:int = 0; i<moverNum; i++){
  11.         movers[i]();
  12.     }
  13. }
  14.  
  15. function makeMover():Function{
  16.     // mover vars & setup
  17.     var xVel:Number = Math.random() * 5 + 1;
  18.     var right:Number = stage.stageWidth + 30;
  19.     var s:Shape = Shape(addChild(new Shape()));
  20.     with(s.graphics) beginFill(0xFF0000), drawCircle(0,0,5);
  21.     s.x = Math.random() * stage.stageWidth;
  22.     s.y = Math.random() * stage.stageHeight;
  23.     // return a "run" function
  24.     return function():void {
  25.          s.x += xVel;
  26.          if (s.x> right){
  27.             s.x = -30;
  28.          }
  29.     }
  30. }

This snippet creates 40 shapes that move to the right at a random velocity. When they animate off the right hand side of the stage they return from the left.

The makeMover() function has a series of variable definitions related to the x velocity and position of a Shape. The makeMover() function then returns an anonymous function that contains some logic to move the Shape "s". The anonymous function has access to the makeMover() functions temporary variables...

On the main loop we run all of the anonymous functions - animating all the shapes by their random x velocities.

It would be interesting to write the same program using classes and test the for speed differences. My assumption is that anonymous functions are expensive, but it would still be interesting to see...

Also posted in motion | Tagged , | Leave a 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