A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: HELP!!! Blowing objects around

  1. #1
    Junior Member
    Join Date
    Jul 2007
    Posts
    13

    HELP!!! Blowing objects around

    How can I make the mouse cursor blow objects around? I have several objects I want to be repelled by the mouse when the mouse travels near it. But I want some of the objects to be blown away from a longer distance than others...

    How would I go about doing this?

    Could somebody please send me a .fla file as I am a beginner in Flash...

    thankyou

    Sinem

  2. #2
    Member
    Join Date
    Nov 2003
    Location
    Ontario, Canada
    Posts
    80
    OooOOOoo a Trig problem most likely. Never done this but I imagine you use
    the mouse._x and mouse._y components with the position of what ever object you are moving.

    so:

    (mouse._x - object._x)^2 + (mouse._y - object._y)^2 = distance_between^2

    if (distance_between < some_value) {

    *****move object function*****
    }


    I dont know if mouse._x, mouse._y is correct, didnt actually check. But I think the logic is right.

  3. #3
    Senior Member hum's Avatar
    Join Date
    Sep 2003
    Location
    CloudCuckooland
    Posts
    1,714
    Hi
    This should help.
    Have a clip on stage called my_mc then this code on timeline.
    Code found here on Flashkit.
    code:
    // Repulsion for a movie clip my_mc from mouse
    // user set properties
    // how close the mouse needs to be before repulsing the movie clip
    my_mc.range = 100;
    // the speed at which the clip is repulsed, 100% speed added when the
    // mouse is directly over the clip, 0% when the mouse is greater than range
    my_mc.speed = 20;
    // friction applied to the clip to slow down by when not within range
    // the closer to 1, the less friction; 1 is no friction
    my_mc.friction = .8;
    // internal properties
    // the movement of the clip, set and used within onEnterFrame
    my_mc.velocity = 0;
    // the angle of the clip, set and used within onEnterFrame
    my_mc.direction = 0;
    // onEnterFrame event controls repulsion
    my_mc.onEnterFrame = function() {
    // dx and dy are the differences in distance of
    // the mouse from this clip
    var dx = this._parent._xmouse-this._x;
    var dy = this._parent._ymouse-this._y;
    // pythagorean theorem used to get actual linear distance
    var distance = Math.sqrt(dx*dx+dy*dy);
    // if the distance is less than the range specified
    // the clip will need to be repulsed
    if (distance<this.range) {
    // assign direction to the angle the mouse is from the clip
    // using dx and dy with atan2
    this.direction = Math.atan2(dy, dx);
    // a proximity ratio is created to represent how close the
    // mouse is to the clip; when closer, more speed is applied
    var proximity = 1-distance/this.range;
    // increase velocity by speed factoring in proximity
    this.velocity += this.speed*proximity;
    }
    // here the clip is actually moved based on its velocity using
    // cos and sin to move based on the direction angle (away from mouse)
    this._x -= this.velocity*Math.cos(this.direction);
    this._y -= this.velocity*Math.sin(this.direction);
    // reduce velocity by friction so the clip won't move for infinity
    this.velocity *= this.friction;
    };
    __________________;


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