Bouncy Balls

Hey! I thought I would start things out with something simple.Its got a few glitchy responses and the code is messy as usual :). Thanks kirupa for holding another AS contest :)! Its been too long since I’ve been here.

_root.createEmptyMovieClip("container",1);
var d = 1;
function addCircle(x, y, scale) {
 var mc = container.attachMovie("Circle", "circle"+d, d++);
 mc._x = x;
 mc._y = y;
 mc._xscale = mc._yscale=scale;
 mc.colorobj = new Color(mc.coloroverlay);
 return mc;
}
MovieClip.prototype.setHue = function(x, mc) {
 //x = 0 - 1
 x = Math.max(x*2*Math.PI, 0.01);
 r = 128+128*Math.cos(x);
 g = 128+128*Math.cos(x+Math.PI*2/3);
 b = 128+128*Math.cos(x+Math.PI*4/3);
 var hex = r << 16 | g << 8 | b;
 this.colorobj.setRGB(hex);
};
var friction = .92;
for (i=0; i<=10; i++) {
 var m = random(70)+30;
 mc = addCircle(random(500), 160, m);
 mc.setHue(0);
 mc.vx = Math.random()*3-1.5;
 mc.vy = Math.random()*3-1.5;
 mc.m = m;
 mc.dragging=false;
 mc.onPress=function(){
  this.dragging=true;
  }
 mc.onRelease=mc.onReleaseOutside=function(){
  this.dragging=false;
  }
 mc.onEnterFrame = function() {
  if (this._x>500-this._width/2 || this._x<this._width/2) {
   this.vx = -this.vx*friction;
  }
  if (this._y>300-this._width/2 || this._y<this._width/2) {
   this.vy = -this.vy*friction;
  }
  this._y = Math.max(this._width/2, Math.min(300-this._width/2, this._y));
  this._x = Math.max(this._width/2, Math.min(500-this._width/2, this._x));
  collision_mc = null;
  var thisv = Math.sqrt(this.vx*this.vx+this.vy*this.vy);
  this.setHue(Math.min(.3,thisv/30));
  for (i in container) {
   var mc = container*;
   if (typeof (mc) == "movieclip" && this != mc) {
    var dx = mc._x-this._x;
    var dy = mc._y-this._y;
    var d = Math.sqrt(dx*dx+dy*dy);
    var thisv = (this.vx*this.vx+this.vy*this.vy);
    var mcv =(mc.vx*mc.vx+mc.vy*mc.vy);
    if (d<this._width/2+mc._width/2 && mcv<thisv) {
     var overlap = mc._width/2+this._width/2-d;
     
     var momentumx2 = mc.m*mc.vx;
     var momentumx1 = this.m*this.vx;
     var momentumy2 = mc.m*mc.vy;
     var momentumy1 = this.m*this.vy;
     dvx = dx/d;
     dvy = dy/d;
     this._x-=dvx*overlap/2;
     this._y-=dvy*overlap/2;
     mc._x+=dvx*overlap/2;
     mc._y+=dvy*overlap/2;
     
     //dot the momentum with the action line
     E1 = this.m*this.vx*dvx+this.m*this.vy*dvy;
     E2 = mc.m*mc.vx*dvx+mc.m*mc.vy*dvy;
     
     momentumx1 += -(E1-E2)*dvx;
     momentumy1 += -(E1-E2)*dvy;
     momentumx2 += (E1-E2)*dvx;
     momentumy2 += (E1-E2)*dvy;
     this.vx = momentumx1/this.m;
     this.vy = momentumy1/this.m;
     mc.vx = momentumx2/mc.m;
     mc.vy = momentumy2/mc.m;
     mc._x+=mc.vx;
     mc._y+=mc.vy;
    }
   }
  }
  this._x += this.vx;
  this._y += this.vy;
  if(!this.dragging)
   this.vy += .2;
  else{
   this.vx=Math.min(10,this._xmouse/5);
   this.vy=Math.min(10,this._ymouse/5);
   }
 };
}

Preview SWF