[COLOR=#000000][FONT=verdana]Hello,[/FONT][/COLOR]
[COLOR=#000000][FONT=verdana]I’m making a car game and I have made so the car moves when I press the keys. But now I want to car to slow down when it drives into the walls. How do I do that? This is my code:
[/FONT][/COLOR]package AS {
import flash.display.MovieClip;
import flash.events.*;
public class Car extends MovieClip {
private var speed:Number = 0;
private var keys:Array = [false,false,false,false];
public function Car()
{
super();
}
public function update(keys:Array):void
{
this.keys = keys;
if (keys[0]) {
speed += 1.5;
}else if(!keys[0]&&!keys[1]){
speed = 0;
}
speed = speed * 0.98;
if (keys[1])
{
speed -= 0.8;
}
if (keys[2])
{
this.rotation += 8;
}
if (keys[3])
{
this.rotation -= 8;
}
this.x += Math.sin (this.rotation * Math.PI / 180) * speed;
this.y += Math.cos (this.rotation * Math.PI / 180) * -speed;
if (Math.abs (speed) > 20)
{
speed = 20;
}
if (speed < -10)
{
speed = -10;
}
}
}
}
[COLOR=#000000][FONT=verdana]
[/FONT][/COLOR]package {
import flash.display.MovieClip;
import flash.events.Event;
import AS.Car;
import flash.events.KeyboardEvent;
public class Main extends MovieClip
{
var timer:CustomTimer;
var car:Car;
private var keys:Array = [false,false,false,false];
public function Main()
{
addEventListener(Event.ENTER_FRAME, update);
trace(stage);
addEventListener(Event.ADDED_TO_STAGE, init);
car = new Car();
addChild(car);
timer = new CustomTimer();
addChild(timer);
}
private function init(e:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
stage.addEventListener(KeyboardEvent.KEY_DOWN, setKeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, setKeyUp);
car.x = stage.width / 2;
car.y = stage.height / 2;
}
public function update(e:Event):void
{
timer.update();
car.update(keys);
}
private function setKeyDown(event:KeyboardEvent):void{
switch(event.keyCode){
case 38:
keys[0] = true;
break;
case 40:
keys[1] = true;
break;
case 39:
keys[2] = true;
break;
case 37:
keys[3] = true;
break;
}
}
private function setKeyUp(event:KeyboardEvent):void{
switch(event.keyCode){
case 38:
keys[0] = false;
break;
case 40:
keys[1] = false;
break;
case 39:
keys[2] = false;
break;
case 37:
keys[3] = false;
break;
}
}
}
}
[COLOR=#000000][FONT=verdana]
Thanks in advance![/FONT][/COLOR]