5 Replies Latest reply on Feb 21, 2011 8:11 PM by prodigalmaster

    Realistic random movement of multiple MovieClips

    prodigalmaster Level 2

      Hi here is some of my code:

      // 30 fps

      var humans = 30;

      var hSpeed = 4;

      for (i=0; i<humans; i++) {
           //attach movieclip with name "human"
           //give it a random x y position (there are now 30 scattered human MCs)
           _root["human"+i].onEnterFrame = function() {
                this._x += random(hSpeed); //Here is the 'random' movement of the Movieclips
                this._x -= random(hSpeed);
                this._y += random(hSpeed);
                this._y -= random(hSpeed);
                     //some more code
           }
      }
      

       

      It makes the movieclips jitter about, humans dont do that, I cant find a way to make their movements random yet smooth, to move in a random direction for a few seconds (preferably a random amout of a few seconds) then to change direction - I found a way to do this but it doesnt work on individuals, it only works on all 30 movieclips meaning they all move in the same direction.

      Remember the movement needs to be random for each individual movieclip, not all of them moving the same direction.

      Does anyone know how? Thanks

        • 1. Re: Realistic random movement of multiple MovieClips
          Ned Murphy Adobe Community Professional & MVP

          If you want to maintain motion in a particular random direction for some length of time, then you are probably better off using Tweening code.  Generate tweens for each object that moves them from their current position to some random new position within some random timeframe.  And have a listener that determines when the tween is complete so that a new tween is intiated with new direction and speed (time).

          • 2. Re: Realistic random movement of multiple MovieClips
            prodigalmaster Level 2

            The tween would need to be interuptted sometimes, because when another mc (zombie) moves near them, the human mc runs away from the zombie mc. Would it be possible for the tween to stop when it is running way from a zombie mc? Also, do you know anyway I can download a tween class for CS3 AS2? I have never used classes so it will be very difficult anyway. Is there another way you can think of or can you help with the tween class? Thanks very much

            • 3. Re: Realistic random movement of multiple MovieClips
              Ned Murphy Adobe Community Professional & MVP

              Flash comes with a built-in Tween class that you can get your feet wet with - you should research it for details on its use.  Most folks will recommend using a third party Tweening class such as TweenMax, etc... and I offer that mainly because of them... I see no problem learning about the built-in as a starting point).

               

              You can tell a Tween to stop if you need to.

               

              Here is some code that moves things around.  I hope you take the time to study it and then on your own work out the other details you mentioned wanting.

               

              import mx.transitions.Tween;

              var maxMove = 50; // change for further random distance moved
              var humans = 30;

               

              var tweenXArray = new Array();
              var tweenYArray = new Array();

               

              // start things off

              for (i=0; i<humans; i++) {
                  makeMove(this["human"+i]);
              }

               

              function makeMove(obj):Void {
                  var twTime = random(2)+1; // 1 to 3 seconds
                  var randX = (obj._x - maxMove) + random(2*maxMove);
                  var randY = (obj._y - maxMove) + random(2*maxMove);

               
                  tweenXArray[i] = new Tween(obj, "_x", null, obj._x, randX, twTime, true);
                  tweenYArray[i] = new Tween(obj, "_y", null, obj._y, randY, twTime, true);

               
                  tweenXArray[i].onMotionFinished = function(){ // use one of the tweens to trigger the next set
                     makeMove(obj);
                  }
              }

              1 person found this helpful
              • 4. Re: Realistic random movement of multiple MovieClips
                prodigalmaster Level 2

                hmm thanks, ive used that code but couldnt get it to work. I changed onMotionFinished to onTweenComplete just because of syntax highlighting, where onMotionComplete is black (wrong colour). Yet it still doesnt work, I got a third party tween class and got it to work slightly, however it always wants to ease in! ah well, I will persevere. Thanks for the help!

                • 5. Re: Realistic random movement of multiple MovieClips
                  prodigalmaster Level 2
                  function newTween() {
                       humansMoving = new Tween(_root["human"+i], "_x", Strong.easeOut, _root["human"+i]._x, _root["human"+i]._x+10, 2, true);
                       trace("yes this function fires");
                  }
                  
                  for (i=0; i<humans; i++) {
                  //blah blah
                       humansMoving = new Tween(_root["human"+i], "_x", Strong.easeOut, _root["human"+i]._x, _root["human"+i]._x+10, 2, true);
                       humansMoving.onMotionFinished = function() {
                            newTween();
                       };
                  

                   

                   

                  This should make a new tween, right? Why doesnt it work? the function newTween() fires because it traces "yes this function works" but the code behind that doesnt work. Is there a way of clearing the old tween when it has ended and creating a new one for the same movieclip?

                  thanks

                  edit: sry for double post