A Flash Developer Resource Site

Results 1 to 9 of 9

Thread: movieclip moves movieclip

  1. #1
    Junior Member
    Join Date
    Feb 2004
    Location
    North Bay, ON
    Posts
    22

    movieclip moves movieclip

    how do i do this:

    i am making a game, and i have a movieclip (a little man) that follows my mouse around the screen. basically i want to create the effect of a 'mosh pit' pushing him around as he tries to get through. i want it so that if he runs into another movie clip (mosher) the mosher will be pushed away from the little man. but i don't want the mosher to continue to move after he's been pushed. maybe just a few pixels at a time. can anyone explain how i would do this? or is this possible?

    btw i am very new to actionscript...lol and i am using flash mx
    Last edited by Rowinski; 03-11-2004 at 03:18 AM.

  2. #2
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    Okay, I've implemented something that does this. It uses one movieclip to represent the mouse-controlled man, and one to represent the moshing people. You can replace those clips with animated clips showing people thrashing about.

    I've attached the complete .fla.

    It contains two movieclips in the library named "man_mc" and "mosher_mc".

    Here is what the script looks like:

    Code:
    // Moshpit 1.0.1
    //
    // - Jim Bumgardner
    
    // function to control position of mouse-man
    function follow_mouse()
    {
    	this._x += (this._parent._xmouse - this._x) / this.sloth;
    	this._y += (this._parent._ymouse - this._y) / this.sloth;
    }
    
    // function to control position of mosher-men
    function mosh_about()
    {
    	if (this.hitTest(mouse_mc)) {
    		this._x -= (mouse_mc._x - this._x) / this.sloth;
    		this._y -= (mouse_mc._y - this._y) / this.sloth;
    	}
    }
    
    // Setup mouse man (or maybe just add him in flash)
    var mc = this.attachMovie("man_mc", "mouse_mc", 0);
    mc._x = 100;     // his position
    mc._y = 50;
    mc.sloth = 20;  // his speed
    mc.onEnterFrame = follow_mouse;
    
    // Setup mosher men in random positions
    for (var i = 0; i < 30; ++i) 
    {
    	mc = this.attachMovie("mosher_mc", "mosh_mc"+i, i+1);
    	mc._x = 50 + random(450);  // mosher man position (random)
    	mc._y = 50 + random(300);
    	mc.sloth = 20;             // mosher man speed
    	mc.onEnterFrame = mosh_about; 
    }
    Attached Files Attached Files

  3. #3
    Junior Member
    Join Date
    Feb 2004
    Location
    North Bay, ON
    Posts
    22
    thanx jbum
    this does what i want to do, but how do i apply it to the man i have already created? i made the little man to change 'positions' depending on where the mouse is by going to different frames within the movie clip, to give the illusion of the little man 'running' or whatever. is there a way i can attach this code or a similar code directly to the actionscript for my movieclip? the one you gave me (and thank you very much) goes in the frame and calls up the man from the library.

  4. #4
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    You should be able to incorporate the mosh_about functionality into the onEnterFrame handler for your little man. This function only affects his _x and _y position, and doesn't care about his frame number.

    Code:
    function mosh_about()
    {
    	if (this.hitTest(mouse_mc)) {
    		this._x -= (mouse_mc._x - this._x) / this.sloth;
    		this._y -= (mouse_mc._y - this._y) / this.sloth;
    	}
    }
    You will need to change the name of mouse_mc to the name of the movie that is following your mouse.

    To simplify things, replace this.sloth with a hardcoded value like 10 or 20.

    I'm realizing this probably isn't much help to you, but without seeing your fla I can't offer more concrete suggestions.

  5. #5
    Junior Member
    Join Date
    Feb 2004
    Location
    North Bay, ON
    Posts
    22
    okay here's the file... i want the security guard to move through a 'mosh pit' (that i haven't created yet). you can see that wherever the mouse is on the screen, he changes positions to follow the mouse. i tried creating some sort of depth illusion too. anyway, if it helps, you can play around with this.
    Attached Files Attached Files

  6. #6
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    Your man is already working fine. We just need to modify the moshers (when you've created them) to run away from him, using functionality similar to what I've written.

    Go ahead and add your moshers and then we'll get them to run away from the man (by modifying their _x and _y coordinates when they detect a collision with the man)...


    Something like this will be needed for
    each mosher (moshist? ):

    Code:
    mosher.onEnterFrame = function()
    {
    	if (this.hitTest(Security)) {
    		this._x -= (Security._x - this._x) / 10;
    		this._y -= (Security._y - this._y) / 10;
    	}
    }
    - jbum
    Last edited by jbum; 03-12-2004 at 03:12 AM.

  7. #7
    Junior Member
    Join Date
    Feb 2004
    Location
    North Bay, ON
    Posts
    22
    thanx again jbum
    i've been playing around with the scripts you gave me, and this is what i came up with. i'm wondering if there's a way i can put the moshers on the "crowd" layer so that they aren't over top of everything else. also, is there a better way to get the depth effect i need, and have the moshers in the foreground overtop of the moshers in the background? take a look and you'll see what i mean.
    Attached Files Attached Files

  8. #8
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    Ordinarily, dynamic content, such as your moshers, appears in the layers above author-time content. You can help fix this using swapDepths();

    You've got a lot of moshers and a few things that you want to put on top of them (spotlights and such). If you give your spotlights (and other foreground elements) instance names, such as spot1_mc, spot2_mc, then in your script you can do this to assign the spots to a higher dynamic layer than the moshers:

    spot1_mc.swapDepths(100);
    spot2_mc.swapDepths(101);

    (Where 100 is a depth layer that is higher than the highest one you want to use for your moshers).

    Getting your moshers to layer properly will be harder. You can try assigning a depth based on the y coodinate (in which low y coordinates correspond to higher depths) using something like:

    this.desired_depth = ???; // insert equation here
    this.swapDepths(this.desired_depth);

    As mosher._y goes up, the moshers get further in the back, and they should have a lower depth number.

    You'll need to fix the equation so that when y is as low as it gets, you get 99 (the highest depth) and when y is as high as it gets (when the moshers are in the back) then you get 1, or the lowest desired depth.

    Here's a general purpose way to do it,
    using some specific (but wrong) numbers:

    Code:
    // these two numbers represent the range of verticle motion for your moshers
    highest_y = 150
    lowest_y = -30
    
    // these two numbers represent the range of depths you want to use
    
    highest_depth = 99
    lowest_depth = 1
    
    // these 3 vars are constants, and can be computed once - all you need is y_to_d
    var y_range = highest_y - lowest_y;
    var d_range = highest_depth - lowest_depth;
    y_to_d = d_range / y_range;
    
    desired_depth = highest_depth  - (y - lowest_y) * y_to_d;
    mosher.swapDepths(desired_depth);
    Last edited by jbum; 03-14-2004 at 01:00 AM.

  9. #9
    Junior Member
    Join Date
    Feb 2004
    Location
    North Bay, ON
    Posts
    22
    thanks a lot jbum

    i tried a bunch of stuff, but i gave up reluctantly, because i would have started throwing stuff. found a simpler way to get the effect i needed. that layers info that you provided was extremely helpful though. thank you very much for your help.

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