Complex Bug, any help please?

This one is even weirder… Move the tank closer to the turrets with the arrow keys, and if your at certain distances… The turrets will flutter.

I did a trace, and I seem to be getting a few NAN’s… It only happens around a certain distance of two to three hundred pixels, and greater than like nine hundred.The nine hundred one I don’t care about, the AI won’t be active then.

Edit… I don’t even beleive it… Another bug! The turrets drift to the right for some reason when they are off screen… What the…

Should I encapsulate everything but the player in a seprate clip or what?
http://photobucket.com/albums/y232/minyMonkey/?action=view&current=tank.swf

I know how to fix the drift bug, put everything that moves inside the land MC. Maybe thats best…


dynamic class enemyTurret extends MovieClip {
    function onLoad() {
        this.life = 20;
        var bullet = 0;
        speed = 15;
        //Duplicate the bullet and fire
        MovieClip.prototype.fireEnemyCannon = function() {
            if (bullet>=200) {
                bullet = 0;
            }
            _root.enemyBullet.duplicateMovieClip("enemyBullet"+(bullet++), bullet+200, {_visible:true, _rotation:this.launch.r, _x:this.launch.x, _y:this.launch.y, gravity:.25, speed:15, friction:.99});
        };
    }
    function onEnterFrame() {
        //Match speed with land
        this._x += _root.landSpeed;
        //Make calculations for use in turret aim.
        angle = this.enemyBunkerTurret0._rotation;
        //Determin fire speeds... For the second time, same code also on bullet... Need to fix that...
        xSpeed = Math.abs(speed*Math.cos(angle*Math.PI/180));
        ySpeed = Math.abs(speed*Math.sin(angle*Math.PI/180));
        //Find distances between tank and turret
        dx = _root.tank0.tankTop0.launch.x-this.enemyBunkerTurret0.enemyLaunchPoint.launch.x;
        dy = _root.tank0.tankTop0.launch.y-this.enemyBunkerTurret0.enemyLaunchPoint.launch.y;
        radians = Math.atan(dy/dx);
        degrees = radians*180/Math.PI;
        degrees += 90;
        if (dx<0) {
            degrees -= 180;
        }
        //Set turret angle, .99is friection, .25 is gravity
        this.enemyBunkerTurret0._rotation = degrees+90-((((dx/xSpeed)*.99)+((dy/ySpeed)*.25))/2);
        //Die animation if life at zero or below.
        if (this.life<=0) {
            i++;
            play();
        }
    }
}