Brownian Movement in Actionscript | Random Motion Tutorial

    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

    Get Adobe 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:

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

    Complex example:

    var numDots = 25;
    var randomRange = 1;

    for(var i=1; i<=numDots; i++) {
    //create a new dot
    duplicateMovieClip(_root.dot, "dot"+i, i);
    //save it's ref path for use
    theDot = _root["dot"+i];
    //give it random coordinates
    theDot._y = Math.random() * Stage.height;
    theDot._x = Math.random() * Stage.width;
    //give each dot a distinct random range
    theDot.randomRange = i/numDots;
    //give each dot a random size and transparency
    theDot._xscale =
    theDot._yscale =
    theDot._alpha =  i*4;

    //apply this code on the dot every frame
    theDot.onEnterFrame = function() {
    //create a random velocity for x and y direction within the specifically created random range for each dot
    vx = Math.random() * this.randomRange - this.randomRange/2;
    vy = Math.random() * this.randomRange - this.randomRange/2;
    //apply velocity to coordinates
    this._x += vx;
    this._y += vy;
    }
    }

    Download

    randomMotion.fla

    This entry was posted in tutorial and tagged , , , , , , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

    2 Comments

    1. andres
      Posted September 11, 2009 at 4:50 pm | Permalink

      Hi, thenks for the file, Ive just applied to my animation, but the thing is that all the random elements are in the first layer, no matter in what layer I put the random element.

      So I cant use it, I really like the visual effect, but how can I do to this stays at the background?

      (Sorry for my english Im still learning

    2. Posted September 13, 2009 at 3:09 pm | Permalink

      @andres – Not sure how you’ve set up your file, but you could try initially putting all the random elements onto their own layer and extending them throughout your entire animation. Sounds like you’re doing some timeline animation? If you have the keyframe on frame 1 and another on your last frame, the script should continue to run throughout your animation.

    Post a Comment

    Your email is never published nor shared. Required fields are marked *

    *
    *

    You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

    • Recent Posts

      WordPress updates plugin directory

      New additions to the plugin directory include: favorites, incorporating support forums into it's own tab for each plugin as well as support stats being displayed! Great! I think we also need the ability to give plugins ratings and reviews (bonus points if it can be done from within a wordpress admin dashboard when installing plugins). [...]

      Short Head

      Use zipf's short head to tune your website rather than redesign the whole thing. To make a website successful it needs to meet the needs of the users. Find out what those needs are by using the short head philosophy to equate most searched things as the biggest needs of the users. Use personas to [...]

      Img Set?

      Great article at a list apart discusing the state of the industry regarding responsive images. This picks apart the set attribute of the img element from a surprisingly objective view coming from someone so close to the picture element. Insightful discussion about the principle behind the proposals than the actual solution too. If the working [...]

      Triudo

      A mesmerizing animated triangle-ish shape form. Embedded Link triduo triduo Tweet

      Git – the paradigm shift

      A great developer story about the differences on what Git is vs other version control and what Git is not. This is how we should learn it. I heard over and over that it was distributed, but never grasped what that meant, so here are a few links and explanations that will help unlearn version [...]

      Tweening Lib comes to Javascript!

      I'm very excited to share the news that the tween library from GreenSock (hands down the best tweening library I used in flash) is not ported for use in javascript! This will be great! I missed that simple syntax from as3 when animating javascript, and now I can have my cake and eat it too. [...]

      Responsive CSS Tricks

      Here are a few useful css tricks to remember when building responsive design sites from web designer wall Embedded Link 5 Useful CSS Tricks for Responsive Design Making the design to be responsive is very easy as shown in my Responsive Design in 3 Steps tutorial, but maintaining the elements to look aesthetically balanced on [...]

      Picture element of srcset attribute?

      Bruce details the reasons and story behind the srcset attribute which is now introduced as an alternative to the picture element. Some aspects of the attribute are nice (like the fact that it's an attribute and not a new element, so it's creating up new elements with for problems. It's adapting currently used elements to [...]

      SVG Preloader with Raphael JS

      Here's a very creative use of using a newly available technology. Using svg graphics which are very lightweight, for a website preloader. I like the animation used as well. Embedded Link Make a stylish preloader with SVG | Tutorial | .net magazine Many sites neglect users with slow connections. Ian Culshaw explains how to use [...]

      CSS3 Button/Icon set

      I've been secretly hoping to see a few of these pop up once the whole icon font idea spread through the nets. I really like this idea and it's a very nice implementation too! I only see some quality issues on a couple of the icons (such as youtube), but it's awesome and I hope [...]

    • Recent Comments

      Bruce Brownlee

      Bruce Brownlee

      Ah IE6. I'd have 2 more years of sleep without IE6. Margin doubling, no properties,...
      versaena

      versaena

      how to give color at runtime…… thank you
      Mobile Websites

      Mobile Websites

      I disagree, mobile websites are the future – desktop websites and mobile websites...
      Matt Fasick

      Matt Fasick

      That's cool. I like the ripple effect as well.
      Nico

      Nico

      hi! really great job guy! very impressive.. just a question… do u have a solution to do a refresh...
      Evan Mullins

      Evan Mullins

      Agreed! I've just seen some people get pretty heated about separating all functionality...