Search code examples
actionscript-3radians

AS3 Rotating movie clip and moving at an angle, results in uncontrollable wobble


I'm making a flash game, top down view shooter sort of thing, and I need the enemies, who are placed randomly on the stage, to turn toward the hero and move towards him. This is all working quite fine, until I move the hero, then the enemies wobble uncontrollably until I stop moving around. I've simplified the code as seen below:

stage.addEventListener(Event.ENTER_FRAME, moveMovie);
function moveMovie(event:Event):void{

movie1.x+= 5 * Math.cos(movie1.rotation);
movie1.y+= 5 * Math.sin(movie1.rotation);
movie1.rotation++;
}

I've tried rotating and then moving, moving and then rotating, putting the movement and rotation into different functions, only rotating if the angle is too big, only rotating every second or so, but still the wobble persists. Am I doing this completely wrong? It seemed simple enough when I tried to implement this.


Solution

  • .rotation property is represented by degrees. Both cos and sin functions accept radians, not degrees. Try like this:

    movie1.x+= 5 * Math.cos(movie1.rotation * Math.PI/180);
    movie1.y+= 5 * Math.sin(movie1.rotation * Math.PI/180);