4 Replies Latest reply on Nov 7, 2006 7:59 AM by kglad

    random movement script

    mrsrowe Level 1
      Hello

      I would like to add a script to individual movie clips to make them move a small amount as if they were hovering, I need to able able to constrain the movement to a small area so as the clips do not hit each other or go out of a particular range.

      I found the script below in "actionscript animation" by keith peters to generate brownian motion: it's very good to generate a visual effect, but I want to have something that I can apply to individual movies which will also be buttons and will trigger events when clicked, I know how to do this, but I don't understand how to make a script that will make the movies hover.

      any help would be appreciated.

      var numDots:Number = 10;
      var friction:Number = 0.95;

      init();
      function init():Void
      {
      for(var i:Number = 0;i<numDots;i++)
      {
      var dot:MovieClip = attachMovie("dot", "dot" + i, i);
      dot._x = Math.random() * 100 - 50 + Stage.width / 2;
      dot._y = Math.random() * 100 - 50 + Stage.height / 2;
      dot.vx = 0;
      dot.vy = 0;
      dot.dot_txt.text = "test";
      }
      }

      function onEnterFrame():Void
      {
      for(var i:Number = 0;i<numDots;i++)
      {
      var dot:MovieClip = this["dot" + i];
      dot.vx += Math.random() * 0.2 - 0.1;
      dot.vy += Math.random() * 0.2 - 0.1;
      dot._x += dot.vx;
      dot._y += dot.vy;
      dot.vx *= friction;
      dot.vy *= friction;

      if(dot._x > Stage.width)
      {
      dot._x = 0;
      }
      else if(dot._x < 0)
      {
      dot._x = Stage.width;
      }
      if(dot._y > Stage.height)
      {
      dot._y = 0;
      }
      else if(dot._y < 0)
      {
      dot._y = Stage.height;
      }
      }
      }