Tinkering: 25 lines of branches

If you haven’t heard, Keith Peters has breathed new life into the old 25 lines Actionscript Contest. The site has now just finished is still taking submissions for the 2nd contest (January 2009). I recommend getting involved in this contest, or at least seeing what you can do with 25 lines of code no matter what programming language you use. I re-worked my previous branching experiment and got it to fit in less than 25 lines, and was even able to add some simple 3D rotation stuff. Very cool. Anyway, here is what I submitted:

Its nice to throw coding conventions and scalability right out the window and just spend time prototyping and playing around.

Not sure how it will fair. Last month had some awesome entries.

Here is the code:

var dots:Array = [], container:Sprite = new Sprite();

container.x = container.y = stage.stageWidth/2;

addChild(container);

dots.push( { sprite: new Sprite(), angle: 0, weight: 50, x: 0, y: 0, z: Math.random()*5 - 2, endX: 0, endY: 0, color: Math.random()*0xFFFFFF, decay: .5, splitChance: 995, kill: false } );

addEventListener( Event.ENTER_FRAME, grow );

// 2 lines... uses a semi colon
stage.addEventListener( MouseEvent.MOUSE_MOVE, function() : void {  container.rotationX = ( stage.mouseY - 600 / 2 ) / 8; container.rotationY = -( stage.mouseX - 600 / 2 ) / 8; });

function grow( evt:Event = null ) : void 
{ 
    if( dots.length == 0) dots.push( { sprite: new Sprite(), angle: 0, weight: 50, x: 0, y: 0, z: Math.random()*50 - 2, endX: 0, endY: 0, color: Math.random()*0xFFFFFF, decay: .5, splitChance: 990, kill: false } )

    for( var el:String in dots)
    {
        if(! container.contains(dots[el].sprite) ) container.addChild( dots[el].sprite );

        ( dots[el].weight > dots[el].decay) ? dots[el].weight -= .2 : dots[el].kill = true;

        dots[el].angle += Math.PI/180*( (Math.random() * dots[el].weight * 20) - dots[el].weight * 10 );
        dots[el].endX += Math.cos( dots[el].angle ) * dots[el].weight/2;
        dots[el].endY += Math.sin( dots[el].angle ) * dots[el].weight/2;
        dots[el].decay -= .01; 

        dots[el].sprite.x = dots[el].x;
        dots[el].sprite.y = dots[el].y;
        dots[el].sprite.graphics.lineStyle(dots[el].weight, dots[el].color -= 3, 1, false, LineScaleMode.NONE, CapsStyle.ROUND, JointStyle.ROUND);
        dots[el].sprite.graphics.lineTo( dots[el].endX, dots[el].endY );        
        
        if( Math.random() * 1000 > dots[el].splitChance ) dots.push( { sprite: new Sprite(), angle: dots[el].angle, weight: dots[el].weight, x: dots[el].endX + dots[el].sprite.x, y: dots[el].endY + dots[el].sprite.y, z: dots[el].sprite.z += dots[el].weight + 1, endX: 0, endY: 0, color: dots[el].color += Math.random()*5000, decay: dots[el].decay * .65, splitChance: dots[el].splitChance += (1000 - dots[el].splitChance)/4, kill: false } )
    }

    var oldDots:Array = dots;
    
    dots = [];

    for( var la:String in oldDots ) if ( !oldDots[la].kill ) dots.push(oldDots[la]);
}

One Response to 'Tinkering: 25 lines of branches'

  1. Pier says:

    Cool Colorful Coding

Leave a Reply to Pier Cancel reply

Your email address will not be published. Required fields are marked *

*