A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: How to hitTest Within an Array?

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    2

    Talking How to hitTest Within an Array?

    Okay so I have this thing where orbs are supposed to stick together, so I put them all in an array, and told them to stick together when they encounter another member of that array. However, they only stick in groups of up to four, and then the orbs begin to overlap and such. Any clue how to fix this, or am I not being clear enough? Thanks!

  2. #2
    Senior Member
    Join Date
    Jun 2003
    Location
    Kent, WA
    Posts
    536
    Can you define "overlapping"? Are the initial four not overlapping? Your existing code would also be very helpful.

  3. #3
    Junior Member
    Join Date
    Jul 2011
    Posts
    2
    Well... they're supposed to stay a certain distance apart from each other, as in if they're far away enough, they get close, but if they're too close, they get farther. They aren't supposed to overlap, but they do anyways. Before I paste my code, I have two more questions... any idea why my clump of orbs has an affinity for moving towards the -x and -y directions? I can't quite figure it out. And the second, is there a way to trace how many MCs an MC is hitting at one point? Thanks again

    Orb Class:
    Actionscript Code:
    class Orb extends MovieClip
    {
        var xvisc; var yvisc;
        var xspeed; var yspeed;
        var momentum;
        var stability;
        var drag:Boolean;
        var size;
        var ratio;
        var base;
        var center;
        var maxspeed
       
        function onLoad()
        {
            xvisc = yvisc = 50;
            xspeed = yspeed = 0;
            momentum = 0.01;
            stability = .5;
            _root.circle.orbs.push(this);
            drag = false;
            size = _width = _height = 50;
            ratio = 2;
            base = _root.attachMovie("OrbBase", "OrbBase" + _root.circle.i, _root.circle.i - 500);
            base._x = _x; base._y = _y;
            center = _root.attachMovie("OrbCenter", "OrbCenter" + _root.circle.i, _root.circle.i + 100);
            center._x = _x; center._y = _y;
            maxspeed = 1
        }
       
        function onEnterFrame()
        {
            _x += xspeed;_y += yspeed;
            if(drag == false)
            {
                if(xspeed > maxspeed){xspeed = maxspeed;}
                if(yspeed > maxspeed){yspeed = maxspeed;}
            }
           
            base._x = _x; base._y = _y;
            base._width = base._height = _width + 10;
            center._x = _x; center._y = _y;
           
            for(var i in _root.circle.orbs)
            {
                if(this.hitTest(_root.circle.orbs[i]))
                {
                    var distance = Math.sqrt(Math.pow(_root.circle.orbs[i]._x - _x, 2) + Math.pow(_root.circle.orbs[i]._y - _y, 2));
                   
                    if(_x > _root.circle.orbs[i]._x){xspeed -= (distance - (size/ratio))/xvisc;}
                    if(_x < _root.circle.orbs[i]._x){xspeed += (distance - (size/ratio))/xvisc;}
                    if(_y > _root.circle.orbs[i]._y){yspeed -= (distance - (size/ratio))/yvisc;}
                    if(_y < _root.circle.orbs[i]._y){yspeed += (distance - (size/ratio))/yvisc;}
                   
                    if(distance < size/ratio + (stability * 5) && distance > size/ratio - (stability * 5))
                    {xspeed -= 0.01; yspeed -= 0.01;}
                }
                else
                {
                    if(xspeed > 0){xspeed -= momentum;} if(yspeed > 0){yspeed -= momentum;}
                    if(xspeed < 0){xspeed += momentum;} if(yspeed < 0){yspeed += momentum;}
                    if(Math.abs(xspeed) < momentum){xspeed = 0;}
                    if(Math.abs(yspeed) < momentum){yspeed = 0;}
                }
            }
           
            if(xspeed < stability && xspeed < 0){xspeed += 0.01;} if(xspeed > stability && xspeed > 0){xspeed -= 0.01;}
            if(yspeed < stability && yspeed < 0){yspeed += 0.01;} if(yspeed > stability && yspeed > 0){yspeed -= 0.01;}
           
            if(Key.isDown(Key.CONTROL))
            {
                this.onPress = function()
                {drag = true;}
            }
            if(!Key.isDown(Key.CONTROL)){drag = false;}
            if(drag == true)
            {
                _x += (_root._xmouse - _x)/10;
                _y += (_root._ymouse - _y)/10;
               
            }
           
        }
       
    }

    And the Circle Class I used to attach and push the orbs:
    Actionscript Code:
    class Circle extends MovieClip
    {
        var orbs;
        var orblimit; var i;
       
        function onLoad()
        {
            orbs = [];
            orblimit = 10; i = 0;
        }
       
        function onEnterFrame()
        {
            this.onPress = function()
            {
                if(!Key.isDown(Key.CONTROL))
                {
                    i++; if(i > orblimit){i = 0;}
                    var neworb = _root.attachMovie("Orb", "Orb" + i, i);
                    neworb._x = _root._xmouse; neworb._y = _root._ymouse;
                }
            }
        }
       
    }

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