# Problem with making a car running after the mouse

**URL:** https://forum.kirupa.com/t/problem-with-making-a-car-running-after-the-mouse/326024
**Category:** programming
**Created:** [November 18, 2011, 3:00am UTC](https://forum.kirupa.com/t/problem-with-making-a-car-running-after-the-mouse/326024 "2011-11-18T03:00:54Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![hwee](https://avatars.discourse-cdn.com/v4/letter/h/f19dbf/32.png) [@hwee](https://forum.kirupa.com/u/hwee)
#### Post date: [November 18, 2011, 3:00am UTC](https://forum.kirupa.com/t/problem-with-making-a-car-running-after-the-mouse/326024/1 "2011-11-18T03:00:54Z")

</div>

I’m trying to make a car to drive toward the cursor. I have a problem that when the car runs toward the mouse, it starts shaking.

This is the 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 \<= 180 && angle \>0) || (angle \< -180)){  
car.rotation += anglesp_sp/10;  
} 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;  
}

function brake(): void {  
sp _= 0.9;  
spx = (-1)spMath.cos(car.rotation_(Math.PI/180));  
spy = (-1)_sp_Math.sin(car.rotation\*(Math.PI/180));  
car.x -= spx;  
car.y -= spy;  
if (sp\<0) {  
sp = 0;  
}  
}

Any ideas to solve the problem? Thanks.
