make this hang in air
A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: make this hang in air

  1. #1
    Senior Member
    Join Date
    Aug 2006
    Posts
    287

    make this hang in air

    hi, can anyone show me where in this script i need to be looking to place the mc named "headmc" to a fixed point on the stage.

    at present all of the clips fall to the bottom of the stage together...

    i want them to all stay connected - but i would like the "headmc" to start and stay at a fixed point on the stage... the other clips then hang around it - as if hung on the wall...

    here is the script that i am working with - if anyone can help me out i'd be really grateful...

    Code:
    	function dist(a, b, c, d)
    	{
        xd = a - c;
        yd = b - d;
        return (Math.sqrt(xd * xd + yd * yd));
    	} // End of the function
    	function dragobj()
    	{
        a = drag;
        xd = _xmouse - a.x;
        yd = _ymouse - a.y;
        dis = dist(_xmouse, _ymouse, a.x, a.y);
        dx = xd / dis;
        dy = yd / dis;
        force = Math.abs((1 - dis) / 2);
        a.xv = a.xv + dx * force;
        a.yv = a.yv + dy * force;
    	} // End of the function
    	function createNode(x, y, xv, yv, r, b, f)
    	{
        nodes.push(new Node(x, y, xv, yv, attachMovie("node", "Node:" + nodeid, getNextHighestDepth(), {_width: r * 2, _height: r * 2}), b, f));
        ++nodeid;
        return (nodes[nodes.length - 1]);
    	} // End of the function
    	function constrainNodes(a, b, len, tolerance, vis, mc)
    	{
        constraints.push(new Constraint(a, b, len, tolerance, vis, mc));
        return (constraints[constraints.length - 1]);
    	} // End of the function
    	function updatesparkdoll()
    	{
        cline.clear();
        if (drag != undefined)
        {
            dragobj();
        } // end if
        for (i = 0; i < nodes.length; i++)
        {
            nodes[i].update(worldspeed);
            n = nodes[i];
            if (n.y > 550)
            {
                n.y = 550;
                n.yv = -Math.abs(n.yv * n.bounce);
                n.xv = n.xv * n.friction;
            } // end if
            if (n.x > 1000)
            {
                n.x = 1000;
                n.xv = -Math.abs(n.xv * n.bounce);
                n.yv = n.yv * n.friction;
                continue;
            } // end if
            if (n.x < 0)
            {
                n.x = 0;
                n.xv = Math.abs(n.xv * n.bounce);
                n.yv = n.yv * n.friction;
            } // end if
        } // end of for
        for (i = 0; i < constraints.length; i++)
        {
            constraints[i].update();
        } // end of for
    
    	} // End of the function
    	var constraints = new Array();
    	var nodes = new Array();
    	var nodeid = 0;
    	var worldspeed = 0.5;
    	var dragthrow = 25;
    	var gravity = 9.000000E-01;
    	var cline = createEmptyMovieClip("constraintline", getNextHighestDepth());
    	onMouseDown = function ()
    	{
        for (i = 0; i < nodes.length; i++)
        {
            if (dist(nodes[i].x, nodes[i].y, _xmouse, _ymouse) < dragthrow)
            {
                drag = nodes[i];
                break;
            } // end if
        } // end of for
    	};
    	onMouseUp = function ()
    		{
        drag = undefined;
    	};
    	head = createNode(random(0) + 275, random(10), 110, 110, 110, 6.000000E-01, 4.000000E-01);
    	shoulder = createNode(random(10) + 275, random(10), 0, 0, 0, 6.000000E-01, 4.000000E-01);
    	lelbow = createNode(random(10) + 275, random(10), 0, 0, 0, 6.000000E-01, 4.000000E-01);
    	lhand = createNode(random(10) + 275, random(10), 0, 0, 0, 6.000000E-01, 4.000000E-01);
    	relbow = createNode(random(10) + 275, random(10), 0, 0, 0, 6.000000E-01, 4.000000E-01);
    	rhand = createNode(random(10) + 275, random(10), 0, 0, 0, 6.000000E-01, 4.000000E-01);
    	pelvis = createNode(random(10) + 275, random(10), 0, 0, 0, 6.000000E-01, 4.000000E-01);
    	lknee = createNode(random(10) + 275, random(10), 0, 0, 0, 6.000000E-01, 4.000000E-01);
    	lfoot = createNode(random(10) + 275, random(10), 0, 0, 0, 6.000000E-01, 4.000000E-01);
    	rknee = createNode(random(10) + 275, random(10), 0, 0, 0, 6.000000E-01, 4.000000E-01);
    	rfoot = createNode(random(10) + 275, random(10), 0, 0, 0, 6.000000E-01, 4.000000E-01);
    	ltoe = createNode(random(10) + 275, random(10), 0, 0, 0, 6.000000E-01, 4.000000E-01);
    	rtoe = createNode(random(10) + 275, random(10), 0, 0, 0, 6.000000E-01, 4.000000E-01);
    constrainNodes(head, shoulder, 15, 100, true, headmc);
    constrainNodes(shoulder, lelbow, 50, 100, true, larmup);
    constrainNodes(lelbow, lhand, 40, 100, true, larmdown);
    constrainNodes(shoulder, relbow, 50, 100, true, rarmup);
    constrainNodes(relbow, rhand, 40, 100, true, rarmdown);
    constrainNodes(shoulder, pelvis, 80, 100, true, body);
    constrainNodes(pelvis, lknee, 60, 100, true, llegup);
    constrainNodes(lknee, lfoot, 55, 100, true, llegdown);
    constrainNodes(pelvis, rknee, 60, 100, true, rlegup);
    constrainNodes(rknee, rfoot, 55, 100, true, rlegdown);
    constrainNodes(rfoot, rtoe, 20, 100, true, rfootmc);
    constrainNodes(lfoot, ltoe, 20, 100, true, lfootmc);
    
    onEnterFrame = function ()
    {
        updatesparkdoll();
    };
    really hope someone can see what i can't
    Attached Files Attached Files

  2. #2
    :
    Join Date
    Dec 2002
    Posts
    3,511
    Code:
    function dist(a, b, c, d) {
            xd = a - c;
            yd = b - d;
            return (Math.sqrt(xd * xd + yd * yd));
    }
    function dragobj() {
            a = drag;
            xd = _xmouse - a.x;
            yd = _ymouse - a.y;
            dis = dist(_xmouse, _ymouse, a.x, a.y);
            dx = xd / dis;
            dy = yd / dis;
            force = Math.abs((1 - dis) / 2);
            a.xv = a.xv + dx * force;
            a.yv = a.yv + dy * force;
    }
    function createNode(x, y, xv, yv, r, b, f) {
            nodes.push(new Node(x, y, xv, yv, attachMovie("node", "Node:" + nodeid, getNextHighestDepth(), {_width:r * 2, _height:r * 2}), b, f));
            ++nodeid;
            return (nodes[nodes.length - 1]);
    }
    function constrainNodes(a, b, len, tolerance, vis, mc) {
            constraints.push(new Constraint(a, b, len, tolerance, vis, mc));
            return (constraints[constraints.length - 1]);
    }
    function updatesparkdoll() {
            cline.clear();
            if (drag != undefined) {
                    dragobj();
            }
            for (i = 0; i < nodes.length; i++) {
                    nodes[i].update(worldspeed);
                    n = nodes[i];
                    if (i == 0) {
                            if (n.y > 330) {
                                    n.y = 330;
                                    n.yv = -Math.abs(n.yv * n.bounce);
                                    n.xv = n.xv * n.friction;
                            }
                    }
                    if (n.y > 550) {
                            n.y = 550;
                            n.yv = -Math.abs(n.yv * n.bounce);
                            n.xv = n.xv * n.friction;
                    }
                    if (n.x > 1000) {
                            n.x = 1000;
                            n.xv = -Math.abs(n.xv * n.bounce);
                            n.yv = n.yv * n.friction;
                            continue;
                    }
                    if (n.x < 0) {
                            n.x = 0;
                            n.xv = Math.abs(n.xv * n.bounce);
                            n.yv = n.yv * n.friction;
                    }
            }
            for (i = 0; i < constraints.length; i++) {
                    constraints[i].update();
            }
    }
    var constraints = new Array();
    var nodes = new Array();
    var nodeid = 0;
    var worldspeed = 0.5;
    var dragthrow = 25;
    var gravity = 1;
    var cline = createEmptyMovieClip("constraintline", getNextHighestDepth());
    
    onMouseDown = function () {
            for (i = 0; i < nodes.length; i++) {
                    if (dist(nodes[i].x, nodes[i].y, _xmouse, _ymouse) < dragthrow) {
                            drag = nodes[i];
                            break;
                    }
            }
    };
    onMouseUp = function () {
            drag = undefined;
    };
    head = createNode(random(10) + 275, random(10), 0, 0, 0, 0.6, 0.4);
    shoulder = createNode(random(10) + 275, random(10), 0, 0, 0, 0.6, 0.4);
    lelbow = createNode(random(10) + 275, random(10), 0, 0, 0, 0.6, 0.4);
    lhand = createNode(random(10) + 275, random(10), 0, 0, 0, 0.6, 0.4);
    relbow = createNode(random(10) + 275, random(10), 0, 0, 0, 0.6, 0.4);
    rhand = createNode(random(10) + 275, random(10), 0, 0, 0, 0.6, 0.4);
    pelvis = createNode(random(10) + 275, random(10), 0, 0, 0, 0.6, 0.4);
    lknee = createNode(random(10) + 275, random(10), 0, 0, 0, 0.6, 0.4);
    lfoot = createNode(random(10) + 275, random(10), 0, 0, 0, 0.6, 0.4);
    rknee = createNode(random(10) + 275, random(10), 0, 0, 0, 0.6, 0.4);
    rfoot = createNode(random(10) + 275, random(10), 0, 0, 0, 0.6, 0.4);
    ltoe = createNode(random(10) + 275, random(10), 0, 0, 0, 0.6, 0.4);
    rtoe = createNode(random(10) + 275, random(10), 0, 0, 0, 0.6, 0.4);
    constrainNodes(head,shoulder,1,100,true,headmc);
    constrainNodes(shoulder,lelbow,50,100,true,larmup);
    constrainNodes(lelbow,lhand,40,100,true,larmdown);
    constrainNodes(shoulder,relbow,50,100,true,rarmup);
    constrainNodes(relbow,rhand,40,100,true,rarmdown);
    constrainNodes(shoulder,pelvis,80,100,true,body);
    constrainNodes(pelvis,lknee,60,100,true,llegup);
    constrainNodes(lknee,lfoot,55,100,true,llegdown);
    constrainNodes(pelvis,rknee,60,100,true,rlegup);
    constrainNodes(rknee,rfoot,55,100,true,rlegdown);
    constrainNodes(rfoot,rtoe,20,100,true,rfootmc);
    constrainNodes(lfoot,ltoe,20,100,true,lfootmc);
    
    onEnterFrame = function () {
            updatesparkdoll();
    };
    Code:
    class Constraint {
            var a, b, dlen, tolerance, vis, self, xd, yd, alen, tempx, tempy, tempxv, tempyv, dx, dy, force;
            function Constraint(a, b, len, tolerance, vis, self) {
                    this.a = a;
                    this.b = b;
                    dlen = len;
                    this.tolerance = tolerance;
                    this.vis = vis;
                    this.self = self;
            }
            function render() {
                    if (self != undefined) {
                            self._x = a.x;
                            self._y = a.y;
                            self._rotation = Math.atan2(a.y - b.y, a.x - b.x) * 180 / 3.141 + 90;
                    } else {
                            _root.cline.lineStyle(16,1,30);
                            _root.cline.moveTo(a.x,a.y);
                            _root.cline.lineTo(b.x,b.y);
                    }
            }
            function breakself() {
                    xd = a.x - b.x;
                    yd = a.y - b.y;
                    alen = Math.sqrt(xd * xd + yd * yd);
                    if (alen > dlen + tolerance) {
                            tempx = a.x;
                            tempy = a.y;
                            tempxv = a.xv;
                            tempyv = a.yv;
                            a = _root.createNode(tempx, tempy, tempxv, tempyv, 0, 0.5, 0.5);
                            tolerance = 1000;
                    }
            }
            function update() {
                    if (vis) {
                            this.render();
                    }
                    this.breakself();
                    xd = a.x + a.xv - (b.x + b.xv);
                    yd = a.y + a.yv - (b.y + b.yv);
                    alen = Math.sqrt(xd * xd + yd * yd);
                    dx = xd / alen;
                    dy = yd / alen;
                    force = dlen - alen;
                    a.xv = a.xv + dx * force / 2;
                    a.yv = a.yv + dy * force / 2;
                    b.xv = b.xv - dx * force / 2;
                    b.yv = b.yv - dy * force / 2;
            }
    }
    Code:
    class Node {
            var x, y, xv, yv, self, bounce, friction;
            function Node(x, y, xv, yv, self, b, f) {
                    this.x = x;
                    this.y = y;
                    this.xv = xv;
                    this.yv = yv;
                    this.self = self;
                    bounce = b;
                    friction = f;
                    self._x = x;
                    self._y = y;
            }
            function update(multiplier) {
                    yv = yv + _root.gravity;
                    x = x + xv * multiplier;
                    y = y + yv * multiplier;
                    self._x = x;
                    self._y = y;
            }
            function cleanup() {
                    self.removeMovieClip();
            }
    }

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

Poll by Flashkit.com