Forums

ForumsProgramming Forum

How to make a car move toward your mouse

Posted Nov 18, '11 at 8:21am

hwee

hwee

3 posts

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;
}

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

 

Posted Nov 19, '11 at 8:18am

hwee

hwee

3 posts

Never mind. I figured it out.

 

Posted Nov 20, '11 at 2:32pm

Darkroot

Darkroot

2,549 posts

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.

 

Posted Nov 21, '11 at 10:19am

hwee

hwee

3 posts

Okay

Here is the solution:

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

 
Reply to How to make a car move toward your mouse

You must be logged in to post a reply!