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 <= 180 && 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; } function brake(): void { sp *= 0.9; 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; if (sp<0) { sp = 0; } } Please 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.
Change this part: 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; //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 <= 180 && 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.