A Flash Developer Resource Site

1. ## random directions

i was making a quick thing where you could use the arrow keys to drive a car around in an arena and wanted to add in other cars that moved around randomly which you could crash into.
i managed to program the player car,

code:
```onClipEvent (enterFrame) {
if (Key.isDown(Key.UP)) {
speed += 2;
} else {
if (Key.isDown(Key.DOWN)) {
speed -= 1;
} else {
speed *= .9;
}
}
if (Math.abs(speed)>25) {
speed *= .6;
}
if (Key.isDown(Key.LEFT)) {
_rotation -= speed;
}
if (Key.isDown(Key.RIGHT)) {
_rotation += speed;
}
speed *= .9;
x = Math.sin(_rotation*(Math.PI/180))*speed;
y = Math.cos(_rotation*(Math.PI/180))*speed*-1;
if (!_root.land.hitTest(_x+x, _y+y, true)) {
_x += x;
_y += y;
} else {
speed *= -.5;
}
}```

but i cant program the computer car to move randomly based upon the same physics.  Reply With Quote

2. Here's one way to do it. This uses a random number from 0-4. If 1,2,3,4 the computer is presing an arrow key, and if 0, the computer is doing nothing.

code:
```
onClipEvent (enterFrame) {
// change action every second
if (lastActionTime == undefined or getTimer() - lastActionTime > 1000)
{
lastActionTime = getTimer();
action = random(5) + 1;
}

if (action == 1) {
speed += 2;
} else {
if (action == 2) {
speed -= 1;
} else {
speed *= .9;
}
}
if (Math.abs(speed)>25) {
speed *= .6;
}
if (action == 3) { // left
_rotation -= speed;
}
if (action == 4)) { // right
_rotation += speed;
}
speed *= .9;
x = Math.sin(_rotation*(Math.PI/180))*speed;
y = Math.cos(_rotation*(Math.PI/180))*speed*-1;
if (!_root.land.hitTest(_x+x, _y+y, true)) {
_x += x;
_y += y;
} else {
speed *= -.5;
}
}
```  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 » Home » Movies » Tutorials » Submissions » Board » Links » Reviews » Feedback » Gallery » Fonts » The Lounge » Sound Loops » Sound FX » About FK » Sitemap 