Home Contact RSS

Brownian Movement in Actionscript | Random Motion Tutorial

1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, average: 4.5 out of 5)
Loading ... Loading ...

Overview

Having things drift around or move randomly has always interested me. Having an animation that is never going to be the exact same thing is very exciting. The focus turns from key-ing exact animations to programming a feel and letting the animations take car of themselves! One type of seemingly random motion is Brownian motion. This gives the movement a random walk wandering look, it will just drift around with no real direction.

Steps

Step by step this process is very simple. In every random motion you create the random number, and apply it to the property. If you want constant random action (motion) rather than just random placement, you repeat that over and over.

  1. Make a random number (random velocity)
  2. Apply the random number (apply velocity to property)
  3. Repeat (if needed)

To create a random number in actionscript, use Math.random(), which creates a random number between 0 and 1. Usually you’ll want to scale it to a range you want to use. If you want a number between 50 and 100, you’d do Math.random() * 50 + 50. *50 to scale it to 0-50, and + 50 to bring it up to 50 - 100. Also if we want to get a 100 range around 0 (-50 - 50) we would do Math.random() * 100 - 50. In the code below I’ve abstracted this to Math.random() * this.randomRange - this.randomRange/2.

Example

(Either JavaScript is not active or you are using an old version of Adobe Flash Player. Please install the newest Flash Player.)
Here I’ve got dots created and placed randomly, with randomly set scale and alpha. On every frame each dot has a random velocity applied to it’s x and y coordinates.
The yellow dot is the simple example (code below) and the rest are included in the complex example below.

Actionscript

Simple Example:

  1. dotOne.onEnterFrame = function() {
  2. //create a random velocity for x and y direction
  3. vx = Math.random() * 4 - 2;
  4. vy = Math.random() * 4 - 2;
  5. //apply velocity to coordinates
  6. this._x += vx;
  7. this._y += vy;
  8. }

Complex example:

  1. var numDots = 25;
  2. var randomRange = 1;
  3.  
  4. for(var i=1; i<=numDots; i++) {
  5. //create a new dot
  6. duplicateMovieClip(_root.dot, "dot"+i, i);
  7. //save it's ref path for use
  8. theDot = _root["dot"+i];
  9. //give it random coordinates
  10. theDot._y = Math.random() * Stage.height;
  11. theDot._x = Math.random() * Stage.width;
  12. //give each dot a distinct random range
  13. theDot.randomRange = i/numDots;
  14. //give each dot a random size and transparency
  15. theDot._xscale =
  16. theDot._yscale =
  17. theDot._alpha =  i*4;
  18.  
  19. //apply this code on the dot every frame
  20. theDot.onEnterFrame = function() {
  21. //create a random velocity for x and y direction within the specifically created random range for each dot
  22. vx = Math.random() * this.randomRange - this.randomRange/2;
  23. vy = Math.random() * this.randomRange - this.randomRange/2;
  24. //apply velocity to coordinates
  25. this._x += vx;
  26. this._y += vy;
  27. }
  28. }

Download

randomMotion.fla

Tags: , , , , , , , ,

Related posts

flashden banner

Leave a Comment