# Help finishing car movement in as3

**URL:** <https://forum.kirupa.com/t/help-finishing-car-movement-in-as3/264553>\
**Category:** flash\
**Created:** [June 27, 2008, 3:56am UTC](https://forum.kirupa.com/t/help-finishing-car-movement-in-as3/264553 "2008-06-27T03:56:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![thejoeknows\_com](https://avatars.discourse-cdn.com/v4/letter/t/5daacb/32.png) [@thejoeknows\_com](https://forum.kirupa.com/u/thejoeknows_com)\
**Post date:** [June 27, 2008, 3:56am UTC](https://forum.kirupa.com/t/help-finishing-car-movement-in-as3/264553/1 "2008-06-27T03:56:34Z")

</div>

im trying to make simple car movement in as3

i think i hav almost got it

i cant seam to get the rotation to work right and the car is “slippery”  
can anyone help?

.fla is attached.

code is here:

```auto

//import some important flash libraries.
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.display.MovieClip;

//initializes variables.
var speed:Number = 0.08;
var xspeed:Number = 0;
var yspeed:Number = 0;
var direction:Number = 0;
var turn:Number = 0;
var friction:Number = 0.98;

var key_left:Boolean = false;
var key_right:Boolean = false;
var key_up:Boolean = false;
var key_down:Boolean = false;

//Checks if the player presses a key.
stage.addEventListener(KeyboardEvent.KEY_DOWN,KeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP,KeyUp);

//Lets the function main play every frame.
addEventListener(Event.ENTER_FRAME,Main);

//create the function main.
function Main(event:Event){
    CheckKeys();
    MoveHero();
    CheckCollision();
}

//create the function KeyDown.
function KeyDown(event:KeyboardEvent){
    if(event.keyCode == 37){ //checks if left arrowkey is pressed.
        key_left = true;
        
                
    }
    if(event.keyCode == 39){ //checks if right arrowkey is pressed.
        key_right = true;
    }
    if(event.keyCode == 38){ //checks if up arrowkey is pressed.
        key_up = true;
    }
    if(event.keyCode == 40){ //checks if down arrowkey is pressed.
        key_down = true;
    }
}

function KeyUp(event:KeyboardEvent){
    if(event.keyCode == 37){ //checks if left arrowkey is released.
        key_left = false;
    }
    if(event.keyCode == 39){ //checks if right arrowkey is released.
        key_right = false;
    }
    if(event.keyCode == 38){ //checks if up arrowkey is released.
        key_up = false;
    }
    if(event.keyCode == 40){ //checks if down arrowkey is released.
        key_down = false;
    }
}

function CheckKeys(){
    if(key_left){
        xspeed -= speed;
        hero.rotation += xspeed;
        turn=1;
    }
    if(key_right){
        xspeed += speed;
        hero.rotation += xspeed;
        turn=1;
    }
    if(key_up){
        yspeed -= speed;
    }
    if(key_down){
        yspeed += speed;
    }
}

function MoveHero(){
    hero.x += xspeed;
    hero.y += yspeed;
    xspeed *= friction;
    yspeed *= friction;
    
    if(!turn){
                rotation=0;
                rotation=0;
            }
    
}

function CheckCollision(){
    //Checks if left border hits the wall.
    if(wall.hitTestPoint(hero.x-(hero.width-5)/2,hero.y,true)){
        hero.x = 275;
        hero.y = 200;
        xspeed = 0;
        yspeed = 0;
    }
    //Checks if right border hits the wall.
    if(wall.hitTestPoint(hero.x+(hero.width-5)/2,hero.y,true)){
        hero.x = 275;
        hero.y = 200;
        xspeed = 0;
        yspeed = 0;
    }
    //Checks if upper border hits the wall.
    if(wall.hitTestPoint(hero.x,hero.y-(hero.height-5)/2,true)){
        hero.x = 275;
        hero.y = 200;
        xspeed = 0;
        yspeed = 0;
    }
    //Checks if lower border hits the wall.
    if(wall.hitTestPoint(hero.x,hero.y+(hero.height-5)/2,true)){
        hero.x = 275;
        hero.y = 200;
        xspeed = 0;
        yspeed = 0;
    }
}

```

---

<div class="post-metadata">

**Author:** ![hasch2o](https://avatars.discourse-cdn.com/v4/letter/h/ba8739/32.png) [@hasch2o](https://forum.kirupa.com/u/hasch2o)\
**Post date:** [June 27, 2008, 6:59am UTC](https://forum.kirupa.com/t/help-finishing-car-movement-in-as3/264553/2 "2008-06-27T06:59:29Z")

</div>

Could you use one thread for your problem? (this is the third that i see for your car problem)  
If you put all in one thread it is easier to see your progress and you don’t get duplicate answers.

cheers Carlo

---

<div class="post-metadata">

**Author:** ![yaim0310](https://avatars.discourse-cdn.com/v4/letter/y/85e7bf/32.png) [@yaim0310](https://forum.kirupa.com/u/yaim0310)\
**Post date:** [June 27, 2008, 7:58pm UTC](https://forum.kirupa.com/t/help-finishing-car-movement-in-as3/264553/3 "2008-06-27T19:58:35Z")

</div>

ignore this file i cant find a way to delete it

---

<div class="post-metadata">

**Author:** ![yaim0310](https://avatars.discourse-cdn.com/v4/letter/y/85e7bf/32.png) [@yaim0310](https://forum.kirupa.com/u/yaim0310)\
**Post date:** [June 28, 2008, 5:57am UTC](https://forum.kirupa.com/t/help-finishing-car-movement-in-as3/264553/4 "2008-06-28T05:57:02Z")

</div>

here is the good one
