A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: random directions

  1. #1
    Junior Member
    Join Date
    Jun 2004
    Posts
    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.
    working with flash 5, please help
    Last edited by RampnRail; 10-15-2004 at 12:57 PM.

  2. #2
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    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;
    }
    }


Posting Permissions

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




Click Here to Expand Forum to Full Width

HTML5 Development Center