Rotating-problem of enemy

Hello folks,

I have this problem: there is this enemy which rotates to my player. While “orbiting” the enemy with my player I can see that the enemy is rotating towards my player.
And then the enemy suddenly turns around 360 degrees and facing to my player again. I don’t know why it does this strange 360 degree turn but it happens everytime when I orbit the enemy for a few seconds. Please - can someone help me out with this problem? I appreciate every hint I can get because I don’t know where the problem might be.

                        
    tempEnemy.dX = tempEnemy.x - player.x;
    tempEnemy.dY = tempEnemy.y - player.y;
                            
    tempEnemy.rotateTo = toDegrees(getRadians(tempEnemy.dX, tempEnemy.dY));
                            
                            
                            
    if(tempEnemy.frame < 0) tempEnemy.frame += 360;
    if(tempEnemy.frame > 359) tempEnemy.frame -= 360;
                            
                            
    tempEnemy.trueRotation = int((tempEnemy.rotateTo - tempEnemy.frame) / tempEnemy.rotateSpeed);
                            
    tempEnemy.vX += (player.x - tempEnemy.x) / tempEnemy._speed;
    tempEnemy.vY += (player.y - tempEnemy.y) / tempEnemy._speed;
             
    tempEnemy.vX *= tempEnemy.decay;
    tempEnemy.vY *= tempEnemy.decay;



Update:



[COLOR=#00008B]private[/COLOR][COLOR=#000000] [/COLOR][COLOR=#00008B]function[/COLOR][COLOR=#000000] toDegrees[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]radians[/COLOR][COLOR=#000000]:[/COLOR][COLOR=#2B91AF]Number[/COLOR][COLOR=#000000]):[/COLOR][COLOR=#2B91AF]Number[/COLOR][COLOR=#000000]
    [/COLOR][COLOR=#000000]{[/COLOR][COLOR=#000000]
        [/COLOR][COLOR=#00008B]var[/COLOR][COLOR=#000000] degrees[/COLOR][COLOR=#000000]:[/COLOR][COLOR=#2B91AF]Number[/COLOR][COLOR=#000000] [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] [/COLOR][COLOR=#2B91AF]Math[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]floor[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]radians [/COLOR][COLOR=#000000]*[/COLOR][COLOR=#000000] [/COLOR][COLOR=#800000]180[/COLOR][COLOR=#000000] [/COLOR][COLOR=#000000]/[/COLOR][COLOR=#000000] [/COLOR][COLOR=#2B91AF]Math[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]PI[/COLOR][COLOR=#000000]);[/COLOR][COLOR=#000000]
        [/COLOR][COLOR=gray]//trace (degrees);[/COLOR][COLOR=#000000]
        [/COLOR][COLOR=#00008B]return[/COLOR][COLOR=#000000] degrees[/COLOR][COLOR=#000000];[/COLOR][COLOR=#000000]
    [/COLOR][COLOR=#000000]}[/COLOR][COLOR=#000000]

[/COLOR][COLOR=#00008B]private[/COLOR][COLOR=#000000] [/COLOR][COLOR=#00008B]function[/COLOR][COLOR=#000000] getRadians[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]deltaX[/COLOR][COLOR=#000000]:[/COLOR][COLOR=#2B91AF]Number[/COLOR][COLOR=#000000],[/COLOR][COLOR=#000000] deltaY[/COLOR][COLOR=#000000]:[/COLOR][COLOR=#2B91AF]Number[/COLOR][COLOR=#000000]):[/COLOR][COLOR=#2B91AF]Number[/COLOR][COLOR=#000000]
    [/COLOR][COLOR=#000000]{[/COLOR][COLOR=#000000]
        [/COLOR][COLOR=#00008B]var[/COLOR][COLOR=#000000] radian[/COLOR][COLOR=#000000]:[/COLOR][COLOR=#2B91AF]Number[/COLOR][COLOR=#000000] [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] [/COLOR][COLOR=#2B91AF]Math[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]atan2[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]deltaY[/COLOR][COLOR=#000000],[/COLOR][COLOR=#000000] deltaX[/COLOR][COLOR=#000000]);[/COLOR][COLOR=#000000]

        [/COLOR][COLOR=#00008B]if[/COLOR][COLOR=#000000] [/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]deltaY [/COLOR][COLOR=#000000]<[/COLOR][COLOR=#000000] [/COLOR][COLOR=#800000]0[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000]
        [/COLOR][COLOR=#000000]{[/COLOR][COLOR=#000000]
            radian [/COLOR][COLOR=#000000]+=[/COLOR][COLOR=#000000] [/COLOR][COLOR=#000000]([/COLOR][COLOR=#800000]2[/COLOR][COLOR=#000000] [/COLOR][COLOR=#000000]*[/COLOR][COLOR=#000000] [/COLOR][COLOR=#2B91AF]Math[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]PI[/COLOR][COLOR=#000000]);[/COLOR][COLOR=#000000]
        [/COLOR][COLOR=#000000]}[/COLOR][COLOR=#000000]
        [/COLOR][COLOR=#00008B]return[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]radian[/COLOR][COLOR=#000000]);[/COLOR][COLOR=#000000]
    [/COLOR][COLOR=#000000]}[/COLOR]