PDA

View Full Version : Help with reset


hirosum
08-16-2010, 05:24 PM
Hey guys I'm not very good at action script and I have a project comin up that I need to get done. Would someone be able to help? I have to create a flash movie in which a car starts in the centre and moves in random directions around the screen, up, down, left, right and diagonally, and then is repositioned to the centre when it gets to the edge of the stage. I have code which I found to make the car move but I need help making it rotate when it turns and reposition itself when it gets to the edge.This is the code I have to make the car move
function carMover(eTarget) {
dir = ["west", "northwest", "southwest", "east", "northeast", "southeast", "north", "south"];
randNum = random(8);
setDir = dir[randNum];
switch (setDir) {
case "west" :
xSpeed = -10;
ySpeed = 0;
break;
case "northwest" :
xSpeed = -10;
ySpeed = 10;
break;
case "southwest" :
xSpeed = -10;
ySpeed = -10;
break;
case "east" :
xSpeed = 10;
ySpeed = 0;
break;
case "northeast" :
xSpeed = 10;
ySpeed = 10;
break;
case "southeast" :
xSpeed = 10;
ySpeed = -10;
break;
case "north" :
xSpeed = 0;
ySpeed = -10;
break;
case "south" :
xSpeed = 0;
ySpeed = 10;
break;
}

this.onEnterFrame = function() {
eTarget._x += xSpeed;
eTarget._y += ySpeed;
eTarget._rotation = rot;
};
}
function callMover() {
_root.carMover(_root.theCar);
}
myInterval = setInterval(callMover, 1000);

Potemkyn
08-18-2010, 09:16 PM
For the edges, you'll need to add to the onEnterFrame function an if set:


if(this._x<10)
{
changeDirection();
}
else if (this._x>Stage.width-10)
{
changeDirection();
}
else if (this._y>Stage.width-10)
{
changeDirection();
}
else if (this._y>Stage.height-10)
{
changeDirection();
}

function changeDirection():Void
{
// stop the car

// rotate the car

// start moving the car

}



HTH

Mike