A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: Random move...orient to path?

Threaded View

  1. #2
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    Here's a rewrite of your code that includes rotation in the direction of movement.

    I also changed the method of detecting if you've reached your destination.

    code:


    acceleration = 10;

    newpos = function ()
    {
    ranx = Math.round((Math.random()*250));
    rany = Math.round((Math.random()*250));
    };

    newpos();

    this.onEnterFrame = function()
    {
    // figure direction of movement, relative to current position
    var dx = ranx-this._x;
    var dy = ranx-this._y;
    // figure angle in radians
    var ar = Math.atan2(dy,dx);
    // convert to rotation in degrees
    this._rotation = ar*180/Math.PI;

    this._x += dx/acceleration;
    this._y += dy/acceleration;
    if (Math.abs(dx) < .5 || Math.abs(dy) < .5) {
    newpos();
    }
    };

    Last edited by jbum; 10-15-2004 at 07:55 PM.

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