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