So I'm using this function that's supposed to make a car change lanes. It will change lanes as long as I don't return the number plate return vehicleObject_list[i]. But when I return the number plate as the instructions ask, the car stops changing lanes return vehicleObject_list[i].Number_Plate. The details are below:
function moveLanes(target_car)
{
/*
This function should do the following:
- move target_car from one lane to the other.
- do the move in a single step without any extra animation.
- use Lane_Position_a and Lane_Position_b to effect the change.
- finally you should return target_car at the end of the function.
hint: You will need to modify the x property of target_car.
*/
if(checkCarInfront(target_car).x == Lane_Position_a ){
target_car.x = Lane_Position_b;
}
else {
target_car.x = Lane_Position_a;
}
}
function checkCarInfront( carObj )
{
/*
This function should do the following:
- determine if carObj is in the same lane and less than 200px behind any of the cars in vehicleObject_list.
- do this by traversing vehicleObject_list and comparing each car's Distance_Driven property to that of carObj.
- if you find a car that matches these requirements then return the Number_Plate property for the car. Otherwise return false.
*/
for (var i = 0; i < vehicleObject_list.length; i++)
{
if (carObj.x == vehicleObject_list[i].x && ((vehicleObject_list[i].Distance_Driven - carObj.Distance_Driven) < 200) && ((vehicleObject_list[i].Distance_Driven - carObj.Distance_Driven) > 0))
{
return vehicleObject_list[i].Number_Plate;
}
}
return false;
}
var vehicleObject_list = [
{ x: 500, y: 0, Distance_Driven: -200, Car_Type: 'greenCar', Number_Plate: 'MBH0WW', Gas_Amt: 2, exhaust: [ ]} , { x: 500, y: 0, Distance_Driven: 200, Car_Type: 'whiteCar', Number_Plate: 'RLDGCM', Gas_Amt: 2, exhaust: [ ]} , { x: 300, y: 0, Distance_Driven: 600, Car_Type: 'whiteCar', Number_Plate: '9WGXXI', Gas_Amt: 2, exhaust: [ ]} ]
Detective_CarObject =
{
x: roadLeftEdge + roadWidth/4,
y: 550,
Distance_Driven: 0,
Gas_Amt: 3,
EngineShudder_Value: 0,
Car_Type: 'detective',
Number_Plate: '5L3UTH',
exhaust: []
}