procedural coding in AS3

OOP is swell and all, but I feel procedural coding still has its place. This is particularly true when you’re not entirely sure what you’re about to make ,or when you have a problem to which you haven’t yet worked out a full solution but are ready to stab at it one bite at a time. This acounts for a hefty percentage of what I do when I sit down in front of flash. Playing; experimenting; or playing Marco Polo with odd math, the drawing api, and a trace window. Moreover, this kind of activity turns out to produce the scaffolding for much of my paid work.

Perhaps I’m marginalized here, but when experimenting or when given a task that begs for a creative solution, I’m not usually ready to write a set of base classes and more classes extending the former. Nor am I one for white-boards, flow-charts, and the like. Rather, I prefer to circumambulate my back-porch, smoke a smok’em-light, and run in and start conducting tests. The long and short of it, again, is that I still do a good bit of procedural coding. I was a little concerned that AS3, being so structured and all, would really start stomping on my procedural lovin’ toes. Now that I’ve had a couple more days to play with it (no knowing where I found the time), I’m pleased to say that I can continue coding ugly, unorganized, and unorthodox. This is hardly to say that I’ve found the time to do anything terribly interresting with AS3 yet. It simply means I’m getting my chops and am just starting to feel somewhat comfortable with diving in with no game plan (an often no real idea what I’m about to make). Here’s an example:
And behold… source in all it filthy procedural glory: 

var w:uint = 550;
var h:uint = 400;
var cos:Function = Math.cos;
var sin:Function = Math.sin;
var canvas:MovieClip = new MovieClip();
parent.addChild(canvas);
var glow:GlowFilter = new GlowFilter();
var bevel:BitmapFilter = new BevelFilter();
glow.strength = .7;
glow.blurX = glow.blurY=4;
bevel.strength = .2;
glow.quality = bevel.quality = 3;
var Filters = [bevel, glow];
function init(e:Event) {
 clickme.visible = false;
 stage.quality = "LOW";
 canvas.filters = [];
 canvas.graphics.clear();
 var type:int = Math.random()*3;
 var v4:int = w/2+Math.random()*250-125;
 var v3:int = h/2+Math.random()*220-110;
 var v11:int = Math.random()*250;
 var v10:int = Math.random()*250;
 var v12:int = Math.random()*250;
 canvas.graphics.moveTo(v4, v3);
 var v5:int = Math.random()*150+10;
 var v6:uint;
 addEventListener("enterFrame", drawIt);
 function drawIt(e:Event) {
  v5 -= 1;
  if(type){
   canvas.graphics.moveTo(w/2, h/2);
  }
  if(v5>0){
   canvas.graphics.lineStyle(0, v6, Math.random()*100);
   var v8:Number = Math.random()*30-15;
   var v7:Number = Math.random()*30-15;
   var v9:Number = Math.random()*30-15;
   v11 += v8;
   v10 += v7;
   v12 += v9;
   v6 = v11 < < 16 | v10 << 8 | v12;
   canvas.graphics.beginFill(v6, Math.random()*100);
   var v2:int = Math.random()*(v5*2)-v5;
   var v1:int = Math.random()*(v5*2)-v5;
   if (v4+v2>w || v4+v2&lt;0) {
    v2 *= -1;
   }
   if (v3+v1>w || v3+v1&lt;0) {
    v1 *= -1;
   }
   (Math.random()*5>1) ? canvas.graphics.curveTo(w/2+v2, h/2+v1, v4, v3) : canvas.graphics.curveTo(v4+v2, v3+v1, v4, v3);
   v4 = w/2+cos(v5)*(v2+100);
   v3 = h/2+sin(v5)*(v1+100);
  }else{
   removeEventListener("enterFrame", drawIt);
   stage.quality = "BEST";
   glow.color = v11/4 < < 16 | v10/4 << 8 | v12/4;
   canvas.filters = Filters;
  }
 }
}
parent.addEventListener("mouseUp", init);

Perhaps ‘filthy’ is a strong word, but one must admit that it has no Class. :P

11 Responses to “procedural coding in AS3”

  1. Paul Neave Says:

    I totally agree. I was brought up on a diet of procedural programming and even to this day find it hard to kick the habit and go entirely OOP loopy. Thankfully the Flash 9 preview release allows timeline code with AS3 so you can still use dirty hackery procedural code but get the AS3 speed boost as well.

  2. Intoxo Pox Says:

    In case anyone reading this missed aral’s post, he’s informed me that a more apt name in this case may be ‘spiking’… eXtreme Programming coinage. Always neat to drop another coin in me lexicon. Thanks aral.

    Check his post out here.
    http://aralbalkan.com/662

  3. Intoxo Pox Says:

    Paul. I confess to similar stubborn habits with respect to OOP.
    It’s funny… I was just toying around with your now ancient pac-man source about a month ago or so after finding my stab at pac-man from a good while back. I also dug your 3d flower generator thing. The internet is offly odd that way. Nice to finally meet you (sort of).

  4. Julien Fondère Says:

    Couldn’t agree more. I’m glad you said it.
    OOP is an incredible tool, but it doesn’t make you a better developer nor does it make your project any better. People need to stop thinking everything should be OOP… Flash is not Java!

  5. John Giotta Says:

    OOP is for final and robust development; its the way to go.

    However, you’re right to say OOP is overkill for exploratory coding.

  6. Bjorn Schultheiss Says:

    Yeah but what happens when you want to reuse that animation in say a RIA that you’re building. Do you Ctrl+C & Ctrl+V till your fingers get sore or would you prefer to instantiate a class out of your animation library?

  7. oh please so Says:

    for those of us poor bastards that have been pounding keys in oop for years suddenly as3 makes everyone a oop finatic — there is a time and a place — some kid get the idea of what a class is then wants to use it for everything — flex is totally case in point — can anyone say overdeveloped — the sdk has promise — as3 is opening a door for massively overdeveloped code

  8. Richard Canfield Says:

    The beautiful design effect reminds me of the german graffiti artist Daim.

    http://www.daimgallery.com

    Coincidence?

  9. Don Lapre James Says:

    I agree that as3 ahouldn’t be pushed aside so quickly. it may take a few to get used to but it more than lives up to the change. Anyway, everyone should live outside the box every now and again, you might actually learn something..
    Jim
    Don Lapre James
    webmaster@gidnoc.com
    http://www.gidnoc.com  

  10. Colin Campbell Says:

    Just getting into as3 now – so far finding it quite, er… “flexible”.

    OOP – a formal method for the mandatory inheritance of bugs.
    I’ve spent far more of my working life going cock-eyed trawling through and refactoring badly designed and/or just plain wrong class-hierarchies, than debugging procedural code.

  11. intoxo poxed » Blog Archive » Yay random! Says:

    [...] eyes. This, as I eventually recalled, was not something new to me… just refreshed. Dig this ancient post of mine from over 6 years ago. It was exciting to circle back and find how far I had come while apparently [...]

Leave a Reply