//carVector
//use vector projection to go any speed, any direction
init();
function init(){
car.speed = 0;
//direction is now in degrees
car.dir = 33;
}
car.onEnterFrame = function(){
checkKeys();
turn(car);
move(car);
} // end enterFrame
function turn(sprite){
//use vector projection to get DX and DY
//offset the angle
degrees = sprite.dir -90;
//convert to radians
radians = degrees / 180 * Math.PI;
//get DX and DY (normalized: length is one)
sprite.dx = Math.cos(radians);
sprite.dy = Math.sin(radians);
//compensate for speed
sprite.dx *= sprite.speed;
sprite.dy *= sprite.speed;
} // end turn;
function move(sprite){
//moves any sprite, wrapping around boundaries
//move
sprite._x += sprite.dx;
sprite._y += sprite.dy;
//rotate changed slightly.
sprite._rotation = sprite.dir;
//check boundaries - wrap all directions
if (sprite._x > Stage.width){
sprite._x = 0;
} // end if
if (sprite._x < 0){
sprite._x = Stage.width;
} // end if
if (sprite._y > Stage.height){
sprite._y = 0;
} // end if
if (sprite._y < 0){
sprite._y = Stage.height;
} // end if
} // end move
function checkKeys(){
//check keyboard to move car
if (Key.isDown(Key.UP)){
car.speed++;
if (car.speed > 8){
car.speed = 8;
} // end if
} // end if
if (Key.isDown(Key.DOWN)){
car.speed--;
if (car.speed < -3){
car.speed = -3;
} // end if
} // end if
if (Key.isDown(Key.RIGHT)){
car.dir += 5;
if (car.dir > 360){
car.dir = 0;
} // end if
} // end if
if (Key.isDown(Key.LEFT)){
car.dir -= 5;
if (car.dir < 0){
car.dir = 360;
} // end if
} // end if
} // end checkKeys