I have a ball game where the ball is suppesed to bounce off of the boundaries to get to the finish(the player draws a set of boundaries and then lets the ball fall and see if it makes it to the finish) similair to paintball at addicting games. It usually works but sometimes the ball goes through the boundaries. It is really bad when i have moving terrain because if the ball is going the opposite dirrection than the terrain it usually goes right through it. Also, I don't know if this can be fixed but sometimes the ball gets inside a line making up the terrain and starts bouncing around inside of the line. I have tried everything from increasing the frame rate to changing the variable values. please help.

heres the code for the ball ( sorry it's so long it repeats once so that the ball bounces off the players terrain as well as the premade terrain):

PHP Code:
onClipEvent (load) {
    
yspeed 0;
    
xspeed 0;
    
gravity 0.2;
    
radius 5;
    
friction 0.90;
    
precision 420;
    
bounces 0;
}
onClipEvent (enterFrame) {
    if (
_root.go == true) {
        
collisions 0;
        
sum_x 0;
        
sum_y 0;
        
yspeed yspeed+gravity;
        for (
x=1x<precisionx++) {
            
spot_x _x+radius*Math.sin(x*360/precision);
            
spot_y _y-radius*Math.cos(x*360/precision);
            if (
_root.terrain.hitTest(spot_xspot_ytrue)) {
                
collisions++;
                
sum_x += spot_x;
                
sum_y += spot_y;
            }
        }
        if (
collisions>0) {
            
_root.last_hit._x old_x;
            
_root.last_hit._y old_y;
            
bounces++;
            
_root.collisions.text "Bounces: "+bounces;
            
ball_dir Math.atan(yspeed/(xspeed*-1))/(Math.PI/180);
            if ((
xspeed*-1)<0) {
                
ball_dir += 180;
            }
            if ((
xspeed*-1)>=&& yspeed<0) {
                
ball_dir += 360;
            }
            
spot_x sum_x/collisions;
            
spot_y sum_y/collisions;
            
x_cat spot_x-_x;
            
y_cat spot_y-_y;
            
ball_coll Math.atan(y_cat/x_cat)/(Math.PI/180);
            if (
x_cat<0) {
                
ball_coll += 180;
            }
            if (
x_cat>=&& y_cat<0) {
                
ball_coll += 360;
            }
            
ground_rotation ball_coll-90;
            if (
ground_rotation<0) {
                
ground_rotation += 180;
            }
            
bounce_angle 180-ball_dir-2*(ground_rotation);
            if (
bounce_angle<0) {
                
bounce_angle += 360;
            }
            
speed Math.sqrt((yspeed*yspeed)+(xspeed*xspeed));
            
xspeed speed*Math.cos(bounce_angle*Math.PI/180)*friction;
            
yspeed = (speed*Math.sin(bounce_angle*Math.PI/180))*-1*friction;
            
_x old_x;
            
_y old_y;
        }
        else{
            
old_x _x;
            
old_y _y;
        }
        
_y _y+yspeed;
        
_x _x+xspeed;
    }
}
onClipEvent (enterFrame) {
    if (
_root.go == true) {
        
collisions 0;
        
sum_x 0;
        
sum_y 0;
        
yspeed yspeed+gravity;
        for (
x=1x<precisionx++) {
            
spot_x _x+radius*Math.sin(x*360/precision);
            
spot_y _y-radius*Math.cos(x*360/precision);
            if (
_root.terrain1.hitTest(spot_xspot_ytrue)) {
                
collisions++;
                
sum_x += spot_x;
                
sum_y += spot_y;
            }
        }
        if (
collisions>0) {
            
_root.last_hit._x old_x;
            
_root.last_hit._y old_y;
            
bounces++;
            
_root.collisions.text "Bounces: "+bounces;
            
ball_dir Math.atan(yspeed/(xspeed*-1))/(Math.PI/180);
            if ((
xspeed*-1)<0) {
                
ball_dir += 180;
            }
            if ((
xspeed*-1)>=&& yspeed<0) {
                
ball_dir += 360;
            }
            
spot_x sum_x/collisions;
            
spot_y sum_y/collisions;
            
x_cat spot_x-_x;
            
y_cat spot_y-_y;
            
ball_coll Math.atan(y_cat/x_cat)/(Math.PI/180);
            if (
x_cat<0) {
                
ball_coll += 180;
            }
            if (
x_cat>=&& y_cat<0) {
                
ball_coll += 360;
            }
            
ground_rotation ball_coll-90;
            if (
ground_rotation<0) {
                
ground_rotation += 180;
            }
            
bounce_angle 180-ball_dir-2*(ground_rotation);
            if (
bounce_angle<0) {
                
bounce_angle += 360;
            }
            
speed Math.sqrt((yspeed*yspeed)+(xspeed*xspeed));
            
xspeed speed*Math.cos(bounce_angle*Math.PI/180)*friction;
            
yspeed = (speed*Math.sin(bounce_angle*Math.PI/180))*-1*friction;
            
_x old_x;
            
_y old_y;
        }
        else{
            
old_x _x;
            
old_y _y;
        }
        
_y _y+yspeed;
        
_x _x+xspeed;
    }
}
onClipEvent (enterFrame) {
    if (
_root.go == true) {
        
collisions 0;
        
sum_x 0;
        
sum_y 0;
        
yspeed yspeed+gravity;
        for (
x=1x<precisionx++) {
            
spot_x _x+radius*Math.sin(x*360/precision);
            
spot_y _y-radius*Math.cos(x*360/precision);
            if (
_root.terrain0.hitTest(spot_xspot_ytrue)) {
                
collisions++;
                
sum_x += spot_x;
                
sum_y += spot_y;
            }
        }
        if (
collisions>0) {
            
_root.last_hit._x old_x;
            
_root.last_hit._y old_y;
            
bounces++;
            
_root.collisions.text "Bounces: "+bounces;
            
ball_dir Math.atan(yspeed/(xspeed*-1))/(Math.PI/180);
            if ((
xspeed*-1)<0) {
                
ball_dir += 180;
            }
            if ((
xspeed*-1)>=&& yspeed<0) {
                
ball_dir += 360;
            }
            
spot_x sum_x/collisions;
            
spot_y sum_y/collisions;
            
x_cat spot_x-_x;
            
y_cat spot_y-_y;
            
ball_coll Math.atan(y_cat/x_cat)/(Math.PI/180);
            if (
x_cat<0) {
                
ball_coll += 180;
            }
            if (
x_cat>=&& y_cat<0) {
                
ball_coll += 360;
            }
            
ground_rotation ball_coll-90;
            if (
ground_rotation<0) {
                
ground_rotation += 180;
            }
            
bounce_angle 180-ball_dir-2*(ground_rotation);
            if (
bounce_angle<0) {
                
bounce_angle += 360;
            }
            
speed Math.sqrt((yspeed*yspeed)+(xspeed*xspeed));
            
xspeed speed*Math.cos(bounce_angle*Math.PI/180)*friction;
            
yspeed = (speed*Math.sin(bounce_angle*Math.PI/180))*-1*friction;
            
_x old_x;
            
_y old_y;
        }
        else{
            
old_x _x;
            
old_y _y;
        }
        
_y _y+yspeed;
        
_x _x+xspeed;
    }

terrain1 is the existing background and terrain is drawn by the player.
i can upload the .fla if that helps.
Any tips or pieces of code would be greatly appreciated. Thank You