Beispiel #1
0
 /**
  * Call this function to see if one <code>FlxObject</code> collides with another.
  * Can be called with one object and one group, or two groups, or two objects,
  * whatever floats your boat!  It will put everything into a quad tree and then
  * check for collisions.  For maximum performance try bundling a lot of objects
  * together using a <code>FlxGroup</code> (even bundling groups together!)
  * NOTE: does NOT take objects' scrollfactor into account.
  *
  * @param	Object1		The first object or group you want to check.
  * @param	Object2		The second object or group you want to check.  If it is the same as the first, flixel knows to just do a comparison within that group.
  */
 public static bool collide(FlxObject Object1, FlxObject Object2)
 {
     if( (Object1 == null) || !Object1.exists ||
         (Object2 == null) || !Object2.exists )
         return false;
     quadTree = new FlxQuadTree(FlxQuadTree.bounds.x,FlxQuadTree.bounds.y,FlxQuadTree.bounds.width,FlxQuadTree.bounds.height,null);
     quadTree.add(Object1,FlxQuadTree.A_LIST);
     bool match = Object1 == Object2;
     if(!match)
         quadTree.add(Object2,FlxQuadTree.B_LIST);
     bool cx = quadTree.overlap(!match,solveXCollision);
     bool cy = quadTree.overlap(!match,solveYCollision);
     return cx || cy;
 }
Beispiel #2
0
 /**
  * Call this function to see if one <code>FlxObject</code> overlaps another.
  * Can be called with one object and one group, or two groups, or two objects,
  * whatever floats your boat!  It will put everything into a quad tree and then
  * check for overlaps.  For maximum performance try bundling a lot of objects
  * together using a <code>FlxGroup</code> (even bundling groups together!)
  * NOTE: does NOT take objects' scrollfactor into account.
  *
  * @param	Object1		The first object or group you want to check.
  * @param	Object2		The second object or group you want to check.  If it is the same as the first, flixel knows to just do a comparison within that group.
  * @param	Callback	A function with two <code>FlxObject</code> parameters - e.g. <code>myOverlapFunction(Object1:FlxObject,Object2:FlxObject);</code>  If no function is provided, <code>FlxQuadTree</code> will call <code>kill()</code> on both objects.
  */
 static public bool overlap(FlxObject Object1, FlxObject Object2, SpriteCollisionEvent Callback)
 {
     if ((Object1 == null) || !Object1.exists ||
         (Object2 == null) || !Object2.exists)
     {
         return(false);
     }
     quadTree = new FlxQuadTree(FlxQuadTree.bounds.x, FlxQuadTree.bounds.y, FlxQuadTree.bounds.width, FlxQuadTree.bounds.height, null);
     quadTree.add(Object1, FlxQuadTree.A_LIST);
     if (Object1 == Object2)
     {
         return(quadTree.overlap(false, Callback));
     }
     quadTree.add(Object2, FlxQuadTree.B_LIST);
     return(quadTree.overlap(true, Callback));
 }
Beispiel #3
0
        /**
         * Call this function to see if one <code>FlxObject</code> collides with another.
         * Can be called with one object and one group, or two groups, or two objects,
         * whatever floats your boat!  It will put everything into a quad tree and then
         * check for collisions.  For maximum performance try bundling a lot of objects
         * together using a <code>FlxGroup</code> (even bundling groups together!)
         * NOTE: does NOT take objects' scrollfactor into account.
         *
         * @param	Object1		The first object or group you want to check.
         * @param	Object2		The second object or group you want to check.  If it is the same as the first, flixel knows to just do a comparison within that group.
         */
        static public bool collide(FlxObject Object1, FlxObject Object2)
        {
            if ((Object1 == null) || !Object1.exists ||
                (Object2 == null) || !Object2.exists)
            {
                return(false);
            }
            quadTree = new FlxQuadTree(FlxQuadTree.bounds.x, FlxQuadTree.bounds.y, FlxQuadTree.bounds.width, FlxQuadTree.bounds.height, null);
            quadTree.add(Object1, FlxQuadTree.A_LIST);
            bool match = Object1 == Object2;

            if (!match)
            {
                quadTree.add(Object2, FlxQuadTree.B_LIST);
            }
            bool cx = quadTree.overlap(!match, solveXCollision);
            bool cy = quadTree.overlap(!match, solveYCollision);

            return(cx || cy);
        }
Beispiel #4
0
 /**
  * Call this function to see if one <code>FlxObject</code> overlaps another.
  * Can be called with one object and one group, or two groups, or two objects,
  * whatever floats your boat!  It will put everything into a quad tree and then
  * check for overlaps.  For maximum performance try bundling a lot of objects
  * together using a <code>FlxGroup</code> (even bundling groups together!)
  * NOTE: does NOT take objects' scrollfactor into account.
  *
  * @param	Object1		The first object or group you want to check.
  * @param	Object2		The second object or group you want to check.  If it is the same as the first, flixel knows to just do a comparison within that group.
  * @param	Callback	A function with two <code>FlxObject</code> parameters - e.g. <code>myOverlapFunction(Object1:FlxObject,Object2:FlxObject);</code>  If no function is provided, <code>FlxQuadTree</code> will call <code>kill()</code> on both objects.
  */
 public static bool overlap(FlxObject Object1, FlxObject Object2, SpriteCollisionEvent Callback)
 {
     if( (Object1 == null) || !Object1.exists ||
         (Object2 == null) || !Object2.exists )
         return false;
     quadTree = new FlxQuadTree(FlxQuadTree.bounds.x,FlxQuadTree.bounds.y,FlxQuadTree.bounds.width,FlxQuadTree.bounds.height, null);
     quadTree.add(Object1,FlxQuadTree.A_LIST);
     if(Object1 == Object2)
         return quadTree.overlap(false,Callback);
     quadTree.add(Object2,FlxQuadTree.B_LIST);
     return quadTree.overlap(true,Callback);
 }
Beispiel #5
0
        /**
         * <code>FlxQuadTree</code>'s other main function.  Call this after adding objects
         * using <code>FlxQuadTree.add()</code> to compare the objects that you loaded.
         *
         * @param	BothLists	Whether you are doing an A-B list comparison, or comparing A against itself.
         * @param	Callback	A function with two <code>FlxObject</code> parameters - e.g. <code>myOverlapFunction(Object1:FlxObject,Object2:FlxObject);</code>  If no function is provided, <code>FlxQuadTree</code> will call <code>kill()</code> on both objects.
         *
         * @return	Whether or not any overlaps were found.
         */
        public bool overlap(bool BothLists, SpriteCollisionEvent Callback)
        {
            _oc = Callback;
            bool    c = false;
            FlxList itr;

            if (BothLists)
            {
                //An A-B list comparison
                _oa = B_LIST;
                if (_headA.@object != null)
                {
                    itr = _headA;
                    while (itr != null)
                    {
                        _o = itr.@object;
                        if (_o.exists && _o.solid && overlapNode())
                        {
                            c = true;
                        }
                        itr = itr.next;
                    }
                }
                _oa = A_LIST;
                if (_headB.@object != null)
                {
                    itr = _headB;
                    while (itr != null)
                    {
                        _o = itr.@object;
                        if (_o.exists && _o.solid)
                        {
                            if ((_nw != null) && _nw.overlapNode())
                            {
                                c = true;
                            }
                            if ((_ne != null) && _ne.overlapNode())
                            {
                                c = true;
                            }
                            if ((_se != null) && _se.overlapNode())
                            {
                                c = true;
                            }
                            if ((_sw != null) && _sw.overlapNode())
                            {
                                c = true;
                            }
                        }
                        itr = itr.next;
                    }
                }
            }
            else
            {
                //Just checking the A list against itself
                if (_headA.@object != null)
                {
                    itr = _headA;
                    while (itr != null)
                    {
                        _o = itr.@object;
                        if (_o.exists && _o.solid && overlapNode(itr.next))
                        {
                            c = true;
                        }
                        itr = itr.next;
                    }
                }
            }

            //Advance through the tree by calling overlap on each child
            if ((_nw != null) && _nw.overlap(BothLists, _oc))
            {
                c = true;
            }
            if ((_ne != null) && _ne.overlap(BothLists, _oc))
            {
                c = true;
            }
            if ((_se != null) && _se.overlap(BothLists, _oc))
            {
                c = true;
            }
            if ((_sw != null) && _sw.overlap(BothLists, _oc))
            {
                c = true;
            }

            return(c);
        }