A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: Make a MC folor another MC

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Location
    India
    Posts
    28

    Make a MC folor another MC

    Hi!
    Can someone please tell me how I can have a movie clip follow another movieclip that is moved with keyboardevents(arrow-keys)
    and make it stop following the movie clip when i press the space bar. then when I press it again it starts following again.
    Thank you!

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Divide your problem into smaller chunks, and solve those. You have 2 tasks:
    1. make one clip follow another one
    2. toggle the follow behavior on spacebar

    To solve the first one, you must define "follow". Does the following clip move only when the followed clip does? Does it maintain a set distance? What happens when the followed clip moves towards the following clip?

    Here's one implementation that assumes that to follow means to approach the followed object on each frame, but keep at least a minimum distance. No action is taken when the followed approaches the follower, even if that may make the distance smaller than the minimum.
    Code:
    var minFollowDist:Number = 50;
    var stepBy:Number = 5; //pixels to move towards the followed object.
    
    //up to you to set these to the appropriate instances
    var followed:DisplayObject;  
    var follower:DisplayObject;
    
    function follow(e:Event):void{
      var xdiff:Number = followed.x - follower.x;
      var ydiff:Number = followed.y - follower.y;
      var dist:Number = Math.sqrt(xdiff*xdiff + ydiff*ydiff); //pythagorean theorem distance formula
      if (dist > minFollowDist){ //if too far apart, move follower closer by stepBy.
        var angle:Number = Math.atan2(ydiff, xdiff);
        follower.x += stepBy*Math.cos(angle);
        follower.y += stepBy*Math.sin(angle);
      }
    }
    To solve the second, you need to know about KeyboardEvents. You probably want to set a KeyboardEvent listener on the stage so that you capture key events no matter what has focus (assuming the swf has focus). You probably want to use KeyboardEvent.KEY_UP so that it doesn't go nuts if you hold the spacebar down.
    Code:
    stage.addEventListener(KeyboardEvent.KEY_UP, toggleFollow);
    Your listener needs to know whether you're currently following or not, so you'll need a boolean variable to track that. This variable needs to be declared in a scope where it will hang around and be useful, so probably on the document class or main timeline.
    Code:
    var following:Boolean = false; //start out not following
    And your function needs to check what key was hit, decide whether to add or remove the follow behavior.
    Code:
    function toggleFollow(e:KeyboardEvent):void{
      if (e.keyCode == 32) {//32 is code for space
        if (following){
          follower.removeEventListener(Event.ENTER_FRAME, follow);
        }else{
          follower.addEventListener(Event.ENTER_FRAME, follow);
        }
        following = !following; //flip boolean
      }// if it's not space, we don't care.
    }

  3. #3
    Junior Member
    Join Date
    Dec 2011
    Location
    India
    Posts
    28
    thanks that works perfectly
    is there a way i can switch to the follower so that i can move it around and let it stay somewhere, then switch over to the folowed mc again?(when the follower doesnt follow the followed MC)

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Switch what?

  5. #5
    Junior Member
    Join Date
    Dec 2011
    Location
    India
    Posts
    28
    i can move and jump with the followed MC (left,right, and jump with the up-key). let's call this mc 'player'.
    and the following mc('follower') follows the player when i press the spacebar.

    now if the follower does not follow the player and i hit for example the s-key i should be able to move the follower (left,right, up and down...without jumping) instead of the player and place the follower somewhere i'd like it to be, then hit 's' again and it stays there and i switch over to the player again.

    i hope it's not too difficult to understand
    Last edited by flip-Ed; 02-09-2012 at 10:17 AM.

  6. #6
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    You already know how to move something with key events, because you're ding that for the followed clip. Simply do the same sort of thing for the follower. You'll need to either remove the listeners which move the player, or have them ignore input while in this state.

  7. #7
    Junior Member
    Join Date
    Dec 2011
    Location
    India
    Posts
    28
    thank you! it works

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center