3 Replies Latest reply on Jul 26, 2006 5:18 PM by kglad

    Moving Characters

    gregmax17
      Hello, I am trying to make several computer characters move around the screen. I am successful in making them walk back and forth between two locations, but it triggers all characters at once. I am asking if anyone knows what I can do to the script provided below to make them turn around and walk back at different 'times' (I guess that is the word to use). here is the script I have so far:

      function turnAround(num) {
      villager_speed = -.3; //ameks the characters walk to the right
      setInterval(walkAround, 4000);
      this['villager_'+num].gotoAndStop(2);
      }

      function walkAround(num) {
      villager_speed = .3; //make them walk to the left
      setInterval(turnAround, 4000, num); //after 4 seconds turn around and walk the other direction
      this['villager_'+num].onEnterFrame = function() {
      this._x -= villager_speed;
      }
      }

      for(var v = 1; v <= 2; v++) { //just two charactesr at the moment
      walkAround(v);
      }

      stop();

      Any help on making them turn around at any random time would be appreciated. Also, the characters have two frames, one looking left and the other looking right. I have this:

      this['villager_'+num].gotoAndStop(2);

      to make them face right when they walk to the right, but it doesn't work...? Please help me with this. Thanks
        • 1. Re: Moving Characters
          kglad Adobe Community Professional & MVP
          don't use that for-loop to initiate walkAround. you can use another setInterval() to stagger villager_1 and villager_2. clear you interval after the 2nd villager is passed to walkAround().
          • 2. Re: Moving Characters
            gregmax17 Level 1
            Ok, this is what I have now:

            function turnAround() {
            villager_speed = -.3;
            turn = setInterval(walkAround, 2000);
            look = 'right';
            }

            function walkAround(num) {
            villager_speed = .3;
            look = 'left';
            setInterval(turnAround, 2000);
            clearInterval(turn);
            this['villager_'+num].onEnterFrame = function() {
            if(look == 'right') { this.gotoAndStop(2); } else { this.gotoAndStop(1); }
            this._x -= villager_speed;
            }
            }


            walkAround(1);
            walkAround(2);

            stop();

            But after a minute (estimating), the villagers stop listening to the function and start flipping random directions. Also, how do you make it so it will make each villager turn at a random time? Thanks
            • 3. Re: Moving Characters
              kglad Adobe Community Professional & MVP
              this will introduce some randomness: