var myShapes:Array = new Array(tl, tr, bl, br);
for each(var shape in myShapes){
var colorInfo:ColorTransform = shape.transform.colorTransform;
colorInfo.color = 0xffffff * Math.random();
shape.transform.colorTransform = colorInfo;
shape.minX = 0;
shape.minY = 0;
shape.maxX = 1024;
shape.maxY = 738;
shape.x = Math.random() * shape.maxX - shape.minX;
shape.y = Math.random() * shape.maxY - shape.minY;
shape.vel = 3;
shape.vx = Math.random() * 15 + 5;
shape.vy = Math.random() * 15 + 5;
addChild(shape);
shape.addEventListener(Event.ENTER_FRAME, animateShapes);
}
function animateShapes(evt:Event):void {
var shape:MovieClip = MovieClip(evt.currentTarget);
shape.posX += shape.velX;
shape.posY += shape.velY;
shape.x += shape.vx;
shape.y += shape.vy;
if ((shape.x + shape.width/2) >= shape.maxX) {
shape.x = shape.maxX - shape.width/2;
shape.vx *= -1;
}
else if ((shape.x - shape.width/2) <= shape.minX) {
shape.x = shape.minX + shape.width/2;
shape.vx *= -1;
}
if ((shape.y + shape.height/2) >= shape.maxY) {
shape.y = shape.maxY - shape.height/2;
shape.vy *= -1;
}
else if ((shape.y - shape.height/2) <= shape.minY) {
shape.y = shape.minY + shape.height/2;
shape.vy *= -1;
}
shape.parent.removeEventListener(Event.ENTER_FRAME , animateShapes);
stage.addEventListener(KeyboardEvent.KEY_UP, makeShapes);
}
function makeShapes(e:KeyboardEvent):void
{
shape.x = shape.vx;
shape.y = shape.vy;
var xMove:Tween = new Tween(shape, "x", Regular.easeIn, shape.vx, 450, 3, true);
var yMove:Tween = new Tween(shape, "y", Regular.easeIn, shape.vy, 450, 3, true);
stage.removeEventListener(KeyboardEvent.KEY_UP, makeShapes);
}