Hello!
I’m in the process of creating a simple AI for a car within a game writen in AS3, I’m not entierly new to AS3, but i’m not that experienced with it either.
I’ve written a function that spawns cars randomly from the stage, here it is:
function SpawnCar(XPos : Number, YPos : Number, arrayIndex : Number) : void
{
//create a car, and pass it the current and next path
trace("Creating a Car @: "+XPos+" : " +YPos);
var car : Car = new Car();
addChild(car);
car.SetCurrentPath(centralPathArray[0]);//set the current path
car.SetNextPath(centralPathArray[1]);//set the next path
car.x = XPos;
car.y = YPos;
currentCars++;
}
That all works fine, its within the car object that has just been created, and had its path’s set that the issue arises.
These are the functions that are called from the code above:
function SetCurrentPath(path : Path) : void
{
currentPath = path;
trace(currentPath.x);
}
function SetNextPath(path : Path) : void
{
nextPath = path;
trace(nextPath.x);
}
The output so far is fine:
Creating a Car @: 140 : 250
405.95
140
However when the enterFrameHandler is called, the output shows the values of both as 0, and they should be the same as the output above.
This is a royal pain as there are no other lines of code anywhere that seem to be removing or changing the values of the nextPath and currentPath variables.
Have i missed something here with AS3?
Thanks.
Here is the eventlistener Code if it helps:
this.addEventListener(Event.ENTER_FRAME,enterFrameHandler);
function enterFrameHandler(e:Event):void
{
//need to rotate the car to face the nextPath
trace(nextPoint.x);
trace(currentPoint,x);
//move the car forwards
carAngle = this.rotation*Math.PI/180;
this.xSpeed = Math.cos(carAngle)*carSpeed;
this.ySpeed = Math.sin(carAngle)*carSpeed;
this.x += this.xSpeed;
this.y += this.ySpeed;
}