var boxes:Array = new Array();
var ballSpeed:Number = -4;
for (var i = 0; i < 9; i++) {
var box:Box = new Box();
box.y = 150;
box.x = box.width * i * 1.5 + 40;
boxes.push(box);
addChild(box);
}
var ball:Ball = new Ball();
ball.x = 320;
ball.y =155;
ball.buttonMode = true;
addChild(ball);
ball.addEventListener(MouseEvent.CLICK, ballClicked);
function ballClicked(e:Event):void {
addEventListener(Event.ENTER_FRAME, enterFrameHandler);
}
function enterFrameHandler(e:Event):void {
ball.x += ballSpeed;
for (var i = 0; i < boxes.length; i++) {
var box:Box = boxes[i];
var distX:Number = ball.x - box.x;
if (distX < 50 && distX > 0 && ballSpeed < 0) {
box.y -= 2;
}
else if (distX < 50 && distX < 0 && ballSpeed < 0) {
if (box.y <= 150) {
box.y += 2;
}
}
if (distX < 0 && distX > -50 && ballSpeed > 0) {
box.y -= 2;
}
else if (distX < 50 && distX > 0 && ballSpeed > 0) {
if (box.y <= 150) {
box.y += 2;
}
}
if (ball.x + 5 >stage.stageWidth || ball.x - 5 < 0) {
ballSpeed *= (-1);
}
}
}