dcsimg
A Flash Developer Resource Site

Results 1 to 9 of 9

Thread: random motion

  1. #1
    supervillain gerbick's Avatar
    Join Date
    Jul 2000
    Location
    undecided.
    Posts
    18,978
    there HAS to be a quicker way to get random movement.
    Code:
    function getdistance (x, y, x1, y1) {
    	var run, rise;
    	run = x1-x;
    	rise = y1-y;
    	return (hyp(run, rise));
    }
    function hyp (a, b) {
    	return (Math.sqrt(a*a+b*b));
    }
    MovieClip.prototype.reset = function () { 
    var dist, norm, movie_height, movie_width;
     
    movie_height = 12;
    movie_width = 310;
    
    speed = Math.random()*4+2;
    targx = Math.random()*(movie_width-_width);
    targy = Math.random()*(movie_height-_height);
    dist = _root.getdistance(_x, _y, targx, targy);
    
    norm = speed/dist;
    diffx = (targx-_x)*norm;
    diffy = (targy-_y)*norm;
    };
    
    
    MovieClip.prototype.move = function () { 
    var cycle;
    
    cycle = 100;
    
    if (_root.getdistance(_x, _y, targx, targy)>speed) {x += diffx;y += diffy;
    } else {x = targx;
    y = targy;
    if (!this.t) {
    t = getTimer();
    }if (getTimer()-t>cycle) {reset();
    t = 0;
    }
    }
    _x = x;
    _y = y;
    }
    then, for each symbol I use
    Code:
             onClipEvent (enterFrame) {
                move();
             }
    there has GOTTA be a quicker method.

  2. #2
    Senior Member
    Join Date
    May 2000
    Posts
    356
    Code:
    onClipEvent (load) { 
    minX = 0; 
    maxX = 550; 
    minY = 0; 
    maxY = 400; 
    speed = 5; 
    xDest = random(maxX - minX) + minX; 
    yDest = random(maxY - minY) + minY;
    } 
    
    onClipEvent (enterFrame) { 
    if (Math.round(_x) != xDest) { 
    _x += xDif; 
    xDif = (xDest - _x) / speed; 
    } else { 
    xDest = random(maxX - minX) + minX; 
    } 
    if (Math.round(_y) != yDest) { 
    _y += yDif; 
    yDif = (yDest - _y) / speed; 
    } else { 
    yDest = random(maxY - minY) + minY; 
    } 
    }
    This might work?

    Josh Dura

  3. #3
    Moderator RazoRmedia's Avatar
    Join Date
    Oct 2000
    Location
    UK
    Posts
    3,016
    gerbick, what are you trying to do?

    Your code above just moves the mc left and right a bit. What is your objective, what do you want it to do?

  4. #4
    supervillain gerbick's Avatar
    Join Date
    Jul 2000
    Location
    undecided.
    Posts
    18,978
    yeah razor... that code is directly from my footer. I want to take a MC, or multiple versions thereof, and basically create TRUE random motion.

    jdura... thanks for the code. but I gotta ask. how do I attach that to a MC?

    yes, you can even treat me like an idiot on the answer

  5. #5
    Moderator RazoRmedia's Avatar
    Join Date
    Oct 2000
    Location
    UK
    Posts
    3,016
    Originally posted by gerbick
    jdura... thanks for the code. but I gotta ask. how do I attach that to a MC?
    create a movieclip, right click on it and choose actions. then paste the text. Don't put it on a frame or it will generate errors.

  6. #6
    Moderator RazoRmedia's Avatar
    Join Date
    Oct 2000
    Location
    UK
    Posts
    3,016
    heres a simpler albeit 'jerkier' version. Speed the frame rate up to 40 and watch it go.

    Code:
    onClipEvent (load) {
        min = "0";
        max = "200";
    }
    onClipEvent (enterFrame) {
        if (this._x <= min) {
            setProperty (this, _x, "100");
        }
        if (this._y <= min) {
            setProperty (this, _y, "100");
        }
        if (this._x >= max) {
            setProperty (this, _x, "100");
        }
        if (this._y >= min) {
            setProperty (this, _y, "100");
        }
        newx = random(20)-10;
        newy = random(20)-10;
        setProperty (this, _x, this._x + newx);
        setProperty (this, _y, this._y + newy);
    }

  7. #7
    Gross Pecululatarian Ed Mack's Avatar
    Join Date
    Dec 2001
    Location
    UK
    Posts
    2,954
    Okay, heres a stripped down version of some code I used ages ago to make "intellegant" movment. The use of trig isn't neccessary.

    Adjust the random to give you the amount of rotation.

    Code:
    onClipEvent (load) {
    	function Move (deg, speed) {
    		var rad = deg*(Math.PI/180);
    		var x = Math.cos(rad)*speed;
    		var y = -Math.sin(rad)*speed;
    		_y += y;
    		_x += x;
    	}
    	speed = 2;
    	angle = Math.random()*360;
    
    	xb = 400;
    	yb = 400;
    }
    onClipEvent (enterFrame) {
    	angle +=( Math.random()*40)-20;
    	Move(angle, speed);
    
    	if(_x < 0 || _x > xb || _y < 0 || _y > yb){
    		angle += 30;
    	}
    }

  8. #8
    Moderator
    The Matrix has you
    2112 F/X

    Join Date
    Jul 2000
    Posts
    1,060
    Originally posted by gerbick
    and basically create TRUE random motion.
    Sounds like what you want is Brownian motion -- a bounded random walk over a 2D lattice. A web search should turn up dozens of pages that will help you out. I used this same technique to animate a flickering flame in MAX using script controllers not too long ago.

    have fun

    too bad I don't speak ActionScript

  9. #9
    Gross Pecululatarian Ed Mack's Avatar
    Join Date
    Dec 2001
    Location
    UK
    Posts
    2,954
    If you want brownian motion, change the random line in my script to this:

    Change
    angle +=( Math.random()*40)-20;

    To
    angle = Math.round(Math.random()*5)==5 ? Math.random()*360 : angle;

    So it makes this:

    Code:
    onClipEvent (load) {
    	function Move (deg, speed) {
    		var rad = deg*(Math.PI/180);
    		var x = Math.cos(rad)*speed;
    		var y = -Math.sin(rad)*speed;
    		_y += y;
    		_x += x;
    	}
    	speed = 10;
    	angle = Math.random()*360;
    
    	xb = 400;
    	yb = 400;
    }
    onClipEvent (enterFrame) {
    	angle = Math.round(Math.random()*2)==2 ? angle + Math.random()*160 + 100 : angle;
    	Move(angle, speed);
    
    	if(_x < 0 || _x > xb || _y < 0 || _y > yb){
    		angle += 30;
    	}
    }

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