I'm trying to make a racing game using mouse control instead of keyboard. I ve already made my car to go in the right direction. However, when it is running in a straight line (directly toward my mouse), it starts to shake.

Here is my code:

//create a click area
var area:Sprite = new Sprite();
area.buttonMode = true;
area.graphics.beginFill(0x000000, 0);
area.graphics.drawRect(0,0,stage.stageWidth, stage.stageHeight);
area.graphics.endFill();
area.addEventListener(MouseEvent.MOUSE_UP, upHandler);
area.addEventListener(MouseEvent.MOUSE_DOWN, downHandler);
addChild(area);
stage.addEventListener(Event.ENTER_FRAME, enterHandler);
function enterHandler(e:Event):void{
if(clicked){
run();
}else{
brake();
}
}

var clicked:Boolean;
function downHandler(e:MouseEvent):void{
clicked = true;
}
function upHandler(e:MouseEvent):void{
clicked = false;
}

//car variables
const maxsp:Number = 20;
const acce:Number =1;
var sp:Number = 0;
var spx:Number;
var spy:Number;
const anglesp:Number = 3;

function run():void {
var angle:Number = getangle(mouseX,mouseY,car.x,car.y)-car.rotation;
if ((angle 0) || (angle < -180)){
car.rotation += anglesp*sp/10; //This is to make the car's rotation speed increases as speed increases.
} else {
car.rotation -= anglesp*sp/10;
}
if (sp < maxsp) {
sp += acce;
} else {
sp = maxsp;
}
spx = (-1)*sp*Math.cos(car.rotation*(Math.PI/180));
spy = (-1)*sp*Math.sin(car.rotation*(Math.PI/180));
car.x -= spx;
car.y -= spy;
}

I'm trying to make a racing game using mouse control instead of keyboard. I ve already made my car to go in the right direction. However, when it is running in a straight line (directly toward my mouse), it starts to shake.\r\n\r\nHere is my code:\r\n\r\n//create a click area\r\n var area:Sprite = new Sprite();\r\n area.buttonMode = true;\r\n area.graphics.beginFill(0x000000, 0);\r\n area.graphics.drawRect(0,0,stage.stageWidth, stage.stageHeight);\r\n area.graphics.endFill();\r\n area.addEventListener(MouseEvent.MOUSE_UP, upHandler);\r\n area.addEventListener(MouseEvent.MOUSE_DOWN, downHandler);\r\n addChild(area);\r\n stage.addEventListener(Event.ENTER_FRAME, enterHandler);\r\n function enterHandler(e:Event):void{\r\n if(clicked){\r\n run();\r\n }else{\r\n brake();\r\n }\r\n }\r\n \r\nvar clicked:Boolean;\r\n function downHandler(e:MouseEvent):void{\r\n clicked = true;\r\n }\r\n function upHandler(e:MouseEvent):void{\r\n clicked = false;\r\n }\r\n \r\n//car variables\r\n const maxsp:Number = 20;\r\n const acce:Number =1;\r\n var sp:Number = 0;\r\n var spx:Number;\r\n var spy:Number;\r\n const anglesp:Number = 3;\r\n \r\nfunction run():void {\r\n var angle:Number = getangle(mouseX,mouseY,car.x,car.y)-car.rotation;\r\n if ((angle <= 180 && angle >0) || (angle < -180)){\r\n car.rotation += anglesp*sp/10; //This is to make the car's rotation speed increases as speed increases.\r\n } else {\r\n car.rotation -= anglesp*sp/10;\r\n } \r\nif (sp < maxsp) {\r\n sp += acce;\r\n } else {\r\n sp = maxsp;\r\n } \r\nspx = (-1)*sp*Math.cos(car.rotation*(Math.PI/180));\r\n spy = (-1)*sp*Math.sin(car.rotation*(Math.PI/180));\r\n car.x -= spx;\r\n car.y -= spy;\r\n }\r\n \r\nfunction brake(): void {\r\n sp *= 0.9;\r\n spx = (-1)*sp*Math.cos(car.rotation*(Math.PI/180));\r\n spy = (-1)*sp*Math.sin(car.rotation*(Math.PI/180));\r\n car.x -= spx;\r\n car.y -= spy;\r\nif (sp<0) {\r\n sp = 0;\r\n }\r\n }\r\n \r\nPlease help me to make it stop shaking. Thank u.

It's probably a good idea if you post the solution in case anyone else has the same problem and stumbles across this thread only to find you saying you figured it out without any solutions.

It's probably a good idea if you post the solution in case anyone else has the same problem and stumbles across this thread only to find you saying you figured it out without any solutions.

Change this part:
function run():void {
var angle:Number = getangle(mouseX,mouseY,car.x,car.y)-car.rotation;
if ((angle 0) || (angle < -180)){
car.rotation += anglesp*sp/10; //This is to make the car's rotation speed increases as speed increases.
} else {
car.rotation -= anglesp*sp/10;
}

To:
function run():void {
var angle:Number = getangle(mouseX,mouseY,car.x,car.y)-car.rotation;
var a:Number = Math.abs(angle);
if (a>180)
a = 360-a;
if ((angle 0) || (angle < -180)){
car.rotation += anglesp*sp*a/200;/*As the small angle between the car and the mouse gets smaller, the car rotates less.*/
} else {
car.rotation -= anglesp*sp*a/200;
}

I'm new to this site and I thought noone read my post so I did not give the solution. :D

Okay\r\n\r\nHere is the solution:\r\n\r\nChange this part:\r\nfunction run():void {\r\nvar angle:Number = getangle(mouseX,mouseY,car.x,car.y)-car.rotation;\r\nif ((angle <= 180 && angle >0) || (angle < -180)){\r\ncar.rotation += anglesp*sp/10; //This is to make the car's rotation speed increases as speed increases.\r\n} else {\r\ncar.rotation -= anglesp*sp/10;\r\n}\r\n\r\nTo:\r\nfunction run():void {\r\nvar angle:Number = getangle(mouseX,mouseY,car.x,car.y)-car.rotation;\r\nvar a:Number = Math.abs(angle);\r\nif (a>180) \r\n a = 360-a;\r\nif ((angle <= 180 && angle >0) || (angle < -180)){\r\ncar.rotation += anglesp*sp*a/200;/*As the small angle between the car and the mouse gets smaller, the car rotates less.*/\r\n} else {\r\ncar.rotation -= anglesp*sp*a/200;\r\n}\r\n\r\nI'm new to this site and I thought noone read my post so I did not give the solution. :D