A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: random movement

  1. #1
    Junior Member
    Join Date
    Jul 2001
    Posts
    5

    random movement

    hi everyone,

    i need some action script to work with random movement. basically i need fish to swim randomly over the movie but also if they change direction the instance movies needs to be flipped.
    Could anyone help me with this problem
    thanks.

  2. #2
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    Do you want us to write you the whole thing from scratch or do you have something you've started that needs work?

    I'm hoping the second one...



    Getting stuff to move randomly around the screen is relatively simple. Getting them to move like real fish is harder. I've done it, using a variation of Craig Reynold's flocking algorithm (look up 'Boids' on Google) but I'm guessing this is a bit more than you're looking for. It would help to have *some idea* what level of realism you're going for.

    - Jim
    Last edited by jbum; 03-10-2004 at 10:32 PM.

  3. #3
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    Ah screw it. Here's a simple (and not very realistic fish) to get you started.

    It does indeed flip horizontally, when necessary.

    Just use this script in a blank movie.

    Note that vx,vy are the fish's "vector" - it's current direction & speed of travel.

    Code:
    MovieClip.prototype.drawFish = function()
    {
      this.moveTo(0,-50);
      this.beginFill(0x0077FF, 100);
      this.lineTo(50,0);
      this.lineTo(0,50);
      this.lineTo(0,-50);
      this.endFill();
      this.moveTo(50,-50);
      this.beginFill(0x00FFFF, 100);
      this.lineTo(200,0);
      this.lineTo(50,50);
      this.lineTo(50,-50);
      this.endFill();
    }
    
    function FishMove()
    {
    	this.vx += Math.random()*10-5;
    	this.vy += Math.random()*10-5;
    	this._x += this.vx;
    	this._y += this.vy;
    	if ((this._x > Stage.width && this.vx > 0) ||
    		 (this._x < 0 && this.vx < 0))
    	{
    		this.vx = -this.vx;
    		this._xscale *= -1;
    	}
    	
    	if ((this._y > Stage.height && this.vy > 0) ||
    		  (this._y < 0 && this.vy < 0))
    		this.vy = -this.vy;
    }
    
    
    var mc = this.createEmptyMovieClip("fish_mc", 0);
    mc._x = mc._y = 100;
    mc._xscale = mc._yscale = 25;
    mc.vx = mv.vy = 0;
    mc.drawFish();
    mc.onEnterFrame = FishMove;
    Here's a fun variant on the last part that you can use to make a bunch of fish (I just put the initialization stuff inside a loop).

    Code:
    for (var i = 0; i < 50; ++i) {
    	var mc = this.createEmptyMovieClip("fish_mc_"+i, i);
    	mc._x = mc._y = 100;
    	mc._xscale = mc._yscale = 25;
    	mc.vx = mv.vy = 0;
    	mc.drawFish();
    	mc.onEnterFrame = FishMove;
    }
    Note that the fish do NOT realistically 'school' - that's what flocking algorithms are for.

    These two lines that modify the movement vector are the ones that you want to tweak to get more interesting kinds of motion:

    this.vx += Math.random()*10-5;
    this.vy += Math.random()*10-5;

    For example, this modification (which increases the rate of change for vx will give you more side-to-side motion and less vertical.

    this.vx += Math.random()*15-7.5;
    this.vy += Math.random()*5-2.5;

    Other way to deal with the speeds is to use constants, like so:

    XSPEED = 15
    YSPEED = 5

    this.vx += Math.random()*XSPEED - XSPEED /2;
    this.vy += Math.random()*YSPEED - YSPEED /2;


    - jim
    Last edited by jbum; 03-10-2004 at 11:43 PM.

  4. #4
    Junior Member
    Join Date
    Jul 2001
    Posts
    5
    cheers ill look into that. thanks heaps. ill keep posted on the progress.

  5. #5
    Junior Member
    Join Date
    Jul 2001
    Posts
    5

    more random trouble

    im still having a bit of trouble.
    im quite new to action scripting and having trouble converting your script to the one if got. i found this script at http://www.kirupa.com/developer/mx/random_motionMX.htm
    ive got the fish objects the way i want them to look and ive tried incorporating my script with yours jim but im not really sure what im doing. im more of a designer than a programmer.
    anyway i like the speed if the one ive got with the flip movement of yours. how would i combine the best of both worlds.

    Tim.

  6. #6
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    In the tutorial script, there's a line that sets diffx:

    this.diffx = (this.targx-this.x)*norm;

    diffx corresponds to my 'vx'.

    When it's positive, you're moving to the right and when it's negative you're moving to the left. So after you modify it, you can set your scaling, like so:

    Code:
    this.diffx = (this.targx-this.x)*norm;
    if (this.diffx < 0)  // moving to left?
      this._xscale = -100;  // mirror image
    else
      this._xscale = 100;
    Incidentally, shortly after responding to your original post, I (heavily) modified my fish script to make a field of windblown grass (for another thread on this board). You can see the results here:

    Windy grass

    - Jim
    Last edited by jbum; 03-11-2004 at 01:03 AM.

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