Results 1 to 4 of 4

Thread: random movement AND make the mc face in the direction that it is moving

  1. #1

    random movement AND make the mc face in the direction that it is moving

    I figured out the random movement part. Well, I didn't figure it out, I looked around. I found one that is way simpler than the tutorial on Kirupa. Btw, this is a game where the player is looking straight down (from a bird's eye type thing). I would like my movieclip to rotate to face the direction that it is moving. Any suggestions?

    Here's the random movement code that I found if anyone is interested...

    Code:
    onClipEvent(load){
        xDir=random(3)-1;
        yDir=random(3)-1;
    }
    onClipEvent(enterFrame){
        if (random(10) == 0){
            xDir=random(3)-1;
        }
        if (random(10) == 0){
            yDir=random(3)-1;
        }
        _x+=xDir;
        _y+=yDir;
    }
    Thanks for the help!

  2. #2
    try using random angles instead.

    Code:
    if (random(10) == 0){
       angle = random(360);
    }
    if (random(10) == 0){
      speed = random(2);
    }
    _x += Math.cos(angle * Math.PI / 180) * speed;
    _y += Math.sin(angle * Math.PI / 180) * speed;
    // x += cos(angle), y += sin(angle) is trig to turn an angle into coordinates
    _rotation = angle;

  3. #3
    To face the direction you are moving do:

    dx and dy are the distance moved since the last frame. So keep variables of the old location (ox, oy)

    dx = x - ox;
    dy = y - oy
    var r = Math.atan2(dy, dx);
    var d = r * 180 / Math.PI-90
    if (d > 180) d -= 360;
    if (d < -180) d += 360;

    obj.rotation = d;

    ox = x;
    oy = y

    if it is too jittery add some smoothing to the movement

  4. #4

    Thanks!

    Quote Originally Posted by tapioca View Post
    try using random angles instead.

    Code:
    if (random(10) == 0){
       angle = random(360);
    }
    if (random(10) == 0){
      speed = random(2);
    }
    _x += Math.cos(angle * Math.PI / 180) * speed;
    _y += Math.sin(angle * Math.PI / 180) * speed;
    // x += cos(angle), y += sin(angle) is trig to turn an angle into coordinates
    _rotation = angle;
    I was thinking of trying to do something like this, but my trig skills just weren't up to the challenge. Thank you very much!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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 About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012