1 var left:Number = 0;
2 var top:Number = 0;
3 var right:Number = stage.stageWidth;
4 var bottom:Number = stage.stageHeight;
5
6 var angle:Number = Math.atan2(ball.vy, ball.vx);
7 var dist:Number = 0;
8 if (ball.x - ball.radius < left)
9 {
10 dist = (ball.x - ball.radius) / Math.cos(angle);
11 ball.y -= Math.sin(angle) * dist;
12 ball.x = ball.radius;
13 ball.vx *= bounce;
14 }
15 else if (ball.x + ball.radius > right)
16 {
17 dist = (ball.radius - (right - ball.x)) / Math.cos(angle);
18 ball.y -= Math.sin(angle) * dist;
19 ball.x = right - ball.radius;
20 ball.vx *= bounce;
21 }
22
23 if (ball.y - ball.radius < top)
24 {
25 dist = (ball.y - ball.radius) / Math.sin(angle);
26 ball.x -= Math.cos(angle) * dist;
27 ball.y = ball.radius;
28 ball.vy *= bounce;
29 }
30 else if (ball.y + ball.radius > bottom)
31 {
32 dist = (ball.radius - (bottom - ball.y)) / Math.sin(angle);
33 ball.x -= Math.cos(angle) * dist;
34 ball.y = bottom - ball.radius;
35 ball.vy *= bounce;
36 }